Skip to content

Commit

Permalink
Resolved the issue where the custom database prefix was lost after th…
Browse files Browse the repository at this point in the history
…e primary cluster created the database.
  • Loading branch information
jellyliao committed Feb 11, 2025
1 parent bb3861f commit a314980
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public AbstractMetaStore(
this.writableDatabaseWhitelist = writableDatabaseWhitelist;
}

public AbstractMetaStore(
String name,
String remoteMetaStoreUris,
String databasePrefix,
AccessControlType accessControlType,
List<String> writableDatabaseWhitelist) {
this.name = name;
this.remoteMetaStoreUris = remoteMetaStoreUris;
this.databasePrefix = databasePrefix;
this.accessControlType = accessControlType;
this.writableDatabaseWhitelist = writableDatabaseWhitelist;
}


public static FederatedMetaStore newFederatedInstance(String name, String remoteMetaStoreUris) {
return new FederatedMetaStore(name, remoteMetaStoreUris);
}
Expand All @@ -95,6 +109,14 @@ public static PrimaryMetaStore newPrimaryInstance(String name, String remoteMeta
return new PrimaryMetaStore(name, remoteMetaStoreUris, AccessControlType.READ_ONLY);
}

public static PrimaryMetaStore newPrimaryInstance(
String name,
String remoteMetaStoreUris,
String databasePrefix,
AccessControlType accessControlType) {
return new PrimaryMetaStore(name, remoteMetaStoreUris, databasePrefix, accessControlType);
}

public String getDatabasePrefix() {
return databasePrefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public PrimaryMetaStore(
super(name, remoteMetaStoreUris, accessControlType, writableDatabaseWhitelist);
}

public PrimaryMetaStore(
String name,
String remoteMetaStoreUris,
String databasePrefix,
AccessControlType accessControlType,
String... writableDatabaseWhitelist) {
this(name, remoteMetaStoreUris, databasePrefix, accessControlType, Arrays.asList(writableDatabaseWhitelist));
}

public PrimaryMetaStore(
String name,
String remoteMetaStoreUris,
String databasePrefix,
AccessControlType accessControlType,
List<String> writableDatabaseWhitelist) {
super(name, remoteMetaStoreUris, databasePrefix, accessControlType, writableDatabaseWhitelist);
}

@Override
public FederationType getFederationType() {
return FederationType.PRIMARY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ public void newPrimaryInstanceWithDefaultAccessControlType() {
assertThat(primaryMetaStore.getAccessControlType(), is(AccessControlType.READ_ONLY));
}

@Test
public void newPrimaryPrefixInstance() {
AccessControlType access = AccessControlType.READ_AND_WRITE_AND_CREATE;
String databasePrefix = "primary_";
PrimaryMetaStore primaryMetaStore = AbstractMetaStore.newPrimaryInstance(name, remoteMetaStoreUri, databasePrefix, access);
assertThat(primaryMetaStore.getName(), is(name));
assertThat(primaryMetaStore.getRemoteMetaStoreUris(), is(remoteMetaStoreUri));
assertThat(primaryMetaStore.getDatabasePrefix(), is(databasePrefix));
assertThat(primaryMetaStore.getAccessControlType(), is(access));
}


@Test
public void mappedDatabases() {
List<String> mappedDatabases = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class PrimaryMetaStoreTest extends AbstractMetaStoreTest<PrimaryMetaStore
private final String remoteMetaStoreUris = "remoteMetaStoreUris";
private final List<String> whitelist = new ArrayList<>();
private final AccessControlType accessControlType = AccessControlType.READ_AND_WRITE_ON_DATABASE_WHITELIST;
private final String databasePrefix = "primary_";

public PrimaryMetaStoreTest() {
super(new PrimaryMetaStore());
Expand Down Expand Up @@ -120,4 +121,29 @@ public void constructorWithArrayListForWhitelist() {
assertThat(store.getWritableDatabaseWhiteList(), is(whitelist));
}

@Test
public void nonEmptyPrefixConstructor() {
whitelist.add("databaseOne");
whitelist.add("databaseTwo");
PrimaryMetaStore store = new PrimaryMetaStore(name, remoteMetaStoreUris, databasePrefix, accessControlType, whitelist.get(0),
whitelist.get(1));
assertThat(store.getName(), is(name));
assertThat(store.getRemoteMetaStoreUris(), is(remoteMetaStoreUris));
assertThat(store.getDatabasePrefix(), is(databasePrefix));
assertThat(store.getAccessControlType(), is(accessControlType));
assertThat(store.getWritableDatabaseWhiteList(), is(whitelist));
}

@Test
public void constructorWithPrefixArrayListForWhitelist() {
whitelist.add("databaseOne");
whitelist.add("databaseTwo");
PrimaryMetaStore store = new PrimaryMetaStore(name, remoteMetaStoreUris, databasePrefix, accessControlType, whitelist);
assertThat(store.getName(), is(name));
assertThat(store.getRemoteMetaStoreUris(), is(remoteMetaStoreUris));
assertThat(store.getDatabasePrefix(), is(databasePrefix));
assertThat(store.getAccessControlType(), is(accessControlType));
assertThat(store.getWritableDatabaseWhiteList(), is(whitelist));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void databaseCreatedNotification(String name) {
AbstractMetaStore newMetaStore;
if (metaStore instanceof PrimaryMetaStore) {
newMetaStore = new PrimaryMetaStore(metaStore.getName(), metaStore.getRemoteMetaStoreUris(),
metaStore.getAccessControlType(), newWritableDatabaseWhiteList);
metaStore.getDatabasePrefix(), metaStore.getAccessControlType(), newWritableDatabaseWhiteList);
newMetaStore.setMappedDatabases(mappedDatabases);
} else {
throw new WaggleDanceException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void databaseCreatedNotification(String name) {
AbstractMetaStore newMetaStore;
if (metaStore instanceof PrimaryMetaStore) {
newMetaStore = new PrimaryMetaStore(metaStore.getName(), metaStore.getRemoteMetaStoreUris(),
metaStore.getAccessControlType());
metaStore.getDatabasePrefix(), metaStore.getAccessControlType());
newMetaStore.setMappedDatabases(mappedDatabases);
} else {
throw new WaggleDanceException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class DatabaseWhitelistAccessControlHandlerTest {
@Before
public void setUp() {
when(primaryMetaStore.getWritableDatabaseWhiteList()).thenReturn(whitelist);
when(primaryMetaStore.getDatabasePrefix()).thenReturn("primary_");
handler = new DatabaseWhitelistAccessControlHandler(primaryMetaStore, federationService, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class ReadWriteCreateAccessControlHandlerTest {
private @Mock FederationService federationService;
private @Captor ArgumentCaptor<PrimaryMetaStore> captor;
private String database = "database";
private String databasePrefix = "primary_";

@Before
public void setUp() {
Expand All @@ -70,6 +71,7 @@ public void databaseCreatedNotificationPrimaryNoMapped() {
PrimaryMetaStore newPrimaryMetaStore = captor.getValue();
assertThat(newPrimaryMetaStore.getMappedDatabases().size(), is(1));
assertThat(newPrimaryMetaStore.getMappedDatabases().get(0), is(database));
assertThat(newPrimaryMetaStore.getDatabasePrefix(), is(databasePrefix));
}

@Test
Expand All @@ -81,6 +83,7 @@ public void databaseCreatedNotificationPrimaryHasEmptyMapped() {
PrimaryMetaStore newPrimaryMetaStore = captor.getValue();
assertThat(newPrimaryMetaStore.getMappedDatabases().size(), is(1));
assertThat(newPrimaryMetaStore.getMappedDatabases().get(0), is(database));
assertThat(newPrimaryMetaStore.getDatabasePrefix(), is(databasePrefix));
}

@Test
Expand All @@ -93,6 +96,7 @@ public void databaseCreatedNotificationPrimaryHasNonEmptyMapped() {
PrimaryMetaStore newPrimaryMetaStore = captor.getValue();
assertThat(newPrimaryMetaStore.getMappedDatabases().size(), is(mappedDatabases.size() + 1));
assertThat(newPrimaryMetaStore.getMappedDatabases().get(mappedDatabases.size()), is(database));
assertThat(newPrimaryMetaStore.getDatabasePrefix(), is(databasePrefix));
}

}

0 comments on commit a314980

Please sign in to comment.