Skip to content

Commit

Permalink
7
Browse files Browse the repository at this point in the history
  • Loading branch information
morningman committed Jan 9, 2025
1 parent fd1a613 commit 2710d6d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public void createDb(CreateDbStmt stmt) throws DdlException {
try {
metadataOps.createDb(stmt);
CreateDbInfo info = new CreateDbInfo(getName(), stmt.getFullDbName(), null);
Env.getCurrentEnv().getEditLog().logNewCreateDb(info);
Env.getCurrentEnv().getEditLog().logCreateDb(info);
} catch (Exception e) {
LOG.warn("Failed to create database {} in catalog {}.", stmt.getFullDbName(), getName(), e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
import org.apache.doris.persist.AlterDatabasePropertyInfo;
import org.apache.doris.persist.AutoIncrementIdUpdateLog;
import org.apache.doris.persist.ColocatePersistInfo;
import org.apache.doris.persist.CreateDbInfo;
import org.apache.doris.persist.DatabaseInfo;
import org.apache.doris.persist.DropDbInfo;
import org.apache.doris.persist.DropInfo;
Expand Down Expand Up @@ -448,7 +449,8 @@ public void createDb(CreateDbStmt stmt) throws DdlException {
}
try {
unprotectCreateDb(db);
Env.getCurrentEnv().getEditLog().logCreateDb(db);
CreateDbInfo dbInfo = new CreateDbInfo(InternalCatalog.INTERNAL_CATALOG_NAME, db.getName(), db);
Env.getCurrentEnv().getEditLog().logCreateDb(dbInfo);
} finally {
db.writeUnlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1419,11 +1419,7 @@ public void logSaveTransactionId(long transactionId) {
logEdit(OperationType.OP_SAVE_TRANSACTION_ID, new Text(Long.toString(transactionId)));
}

public void logCreateDb(Database db) {
logEdit(OperationType.OP_CREATE_DB, db);
}

public void logNewCreateDb(CreateDbInfo info) {
public void logCreateDb(CreateDbInfo info) {
logEdit(OperationType.OP_NEW_CREATE_DB, info);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class OperationType {
// OP_LOCAL_EOF is only for local edit log, to indicate the end of a edit log run.
public static final short OP_LOCAL_EOF = -1;
public static final short OP_SAVE_NEXTID = 0;
public static final short OP_CREATE_DB = 1;
@Deprecated
public static final short OP_CREATE_DB = 1; // deprecated, use OP_NEW_CREATE_DB instead
public static final short OP_DROP_DB = 2;
public static final short OP_ALTER_DB = 3;
public static final short OP_ERASE_DB = 4;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.persist;

import org.apache.doris.catalog.Database;
import org.apache.doris.common.FeMetaVersion;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.meta.MetaContext;

import org.junit.Assert;
import org.junit.Test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.nio.file.Files;

public class CreateDbInfoTest {
@Test
public void testSerialization() throws Exception {
MetaContext metaContext = new MetaContext();
metaContext.setMetaVersion(FeMetaVersion.VERSION_CURRENT);
metaContext.setThreadLocalInfo();

// 1. Write objects to file
File file = new File("./createDbInfo");
file.createNewFile();
DataOutputStream dos = new DataOutputStream(Files.newOutputStream(file.toPath()));

Database db = new Database(10000, "db1");
CreateDbInfo info1 = new CreateDbInfo(InternalCatalog.INTERNAL_CATALOG_NAME, db.getName(), db);
info1.write(dos);

CreateDbInfo info2 = new CreateDbInfo("external_catalog", "external_db", null);
info2.write(dos);

dos.flush();
dos.close();

// 2. Read objects from file
DataInputStream dis = new DataInputStream(Files.newInputStream(file.toPath()));

CreateDbInfo rInfo1 = CreateDbInfo.read(dis);
Assert.assertEquals(info1.getCtlName(), rInfo1.getCtlName());
Assert.assertEquals(info1.getDbName(), rInfo1.getDbName());
Assert.assertEquals(info1.getInternalDb().getId(), rInfo1.getInternalDb().getId());

CreateDbInfo rInfo2 = CreateDbInfo.read(dis);
Assert.assertEquals(info2.getCtlName(), rInfo2.getCtlName());
Assert.assertEquals(info2.getDbName(), rInfo2.getDbName());
Assert.assertNull(rInfo2.getInternalDb());

// 3. delete files
dis.close();
file.delete();
}
}

0 comments on commit 2710d6d

Please sign in to comment.