Skip to content

Commit

Permalink
fix: issue with replication after creating a database on the leader
Browse files Browse the repository at this point in the history
Fixed #1207
  • Loading branch information
lvca committed Aug 14, 2023
1 parent 407ab9d commit efa307a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
23 changes: 11 additions & 12 deletions server/src/main/java/com/arcadedb/server/ArcadeDBServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ public boolean existsDatabase(final String databaseName) {
return databases.containsKey(databaseName);
}

public DatabaseInternal createDatabase(final String databaseName, final ComponentFile.MODE mode) {
DatabaseInternal db;
public ServerDatabase createDatabase(final String databaseName, final ComponentFile.MODE mode) {
ServerDatabase serverDatabase;
synchronized (databases) {
db = databases.get(databaseName);
if (db != null)
serverDatabase = databases.get(databaseName);
if (serverDatabase != null)
throw new IllegalArgumentException("Database '" + databaseName + "' already exists");

final DatabaseFactory factory = new DatabaseFactory(
Expand All @@ -345,23 +345,22 @@ public DatabaseInternal createDatabase(final String databaseName, final Componen
if (factory.exists())
throw new IllegalArgumentException("Database '" + databaseName + "' already exists");

db = (DatabaseInternal) factory.create();
DatabaseInternal embeddedDatabase = (DatabaseInternal) factory.create();

if (mode == READ_ONLY) {
db.close();
db = (DatabaseInternal) factory.open(mode);
embeddedDatabase.close();
embeddedDatabase = (DatabaseInternal) factory.open(mode);
}

if (configuration.getValueAsBoolean(GlobalConfiguration.HA_ENABLED))
db = new ReplicatedDatabase(this, (EmbeddedDatabase) db);
embeddedDatabase = new ReplicatedDatabase(this, (EmbeddedDatabase) embeddedDatabase);

db = new ServerDatabase(db);
serverDatabase = new ServerDatabase(embeddedDatabase);

// FORCE LOADING INTO THE SERVER
databases.put(databaseName, (ServerDatabase) db);
databases.put(databaseName, serverDatabase);
return serverDatabase;
}

return db;
}

public Set<String> getDatabaseNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public ExecutionResponse execute(final HttpServerExchange exchange, final Server

final DatabaseInternal db = server.createDatabase(databaseName, ComponentFile.MODE.READ_WRITE);

if (server.getConfiguration().getValueAsBoolean(GlobalConfiguration.HA_ENABLED))
((ReplicatedDatabase) db).createInReplicas();
if (server.getConfiguration().getValueAsBoolean(GlobalConfiguration.HA_ENABLED)) {
final ReplicatedDatabase replicatedDatabase = (ReplicatedDatabase) db.getEmbedded();
replicatedDatabase.createInReplicas();
}

return new ExecutionResponse(200, "{ \"result\" : \"ok\"}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.arcadedb.serializer.json.JSONArray;
import com.arcadedb.serializer.json.JSONObject;
import com.arcadedb.server.ArcadeDBServer;
import com.arcadedb.server.ServerDatabase;
import com.arcadedb.server.ha.HAServer;
import com.arcadedb.server.ha.Leader2ReplicaNetworkExecutor;
import com.arcadedb.server.ha.Replica2LeaderNetworkExecutor;
Expand Down Expand Up @@ -173,10 +174,12 @@ private void createDatabase(final String command) {
final ArcadeDBServer server = httpServer.getServer();
server.getServerMetrics().meter("http.create-database").hit();

final DatabaseInternal db = server.createDatabase(databaseName, ComponentFile.MODE.READ_WRITE);
final ServerDatabase db = server.createDatabase(databaseName, ComponentFile.MODE.READ_WRITE);

if (server.getConfiguration().getValueAsBoolean(GlobalConfiguration.HA_ENABLED))
((ReplicatedDatabase) db).createInReplicas();
if (server.getConfiguration().getValueAsBoolean(GlobalConfiguration.HA_ENABLED)) {
final ReplicatedDatabase replicatedDatabase = (ReplicatedDatabase) db.getEmbedded();
replicatedDatabase.createInReplicas();
}
}

private ExecutionResponse getServerEvents(final String command) {
Expand Down

0 comments on commit efa307a

Please sign in to comment.