Yuta HIGUCHI

Keep retrying until Database table is ready.

Change-Id: Idd696e3807435c65a422064e078cf5993a216df1
......@@ -104,33 +104,32 @@ public class DistributedLinkResourceStore implements LinkResourceStore {
private StoreSerializer serializer;
@Activate
public void activate() {
serializer = new KryoSerializer();
Set<String> tables = null;
int retries = 0;
void createTable(String tableName) {
boolean tableReady = false;
do {
try {
tables = databaseAdminService.listTables();
} catch (DatabaseException e) {
log.debug("DatabaseException", e);
retries++;
if (retries > 10) {
log.error("Failed to list tables, moving on", e);
tables = new HashSet<>();
if (!databaseAdminService.listTables().contains(tableName)) {
databaseAdminService.createTable(tableName);
}
tableReady = true;
} catch (DatabaseException e) {
log.debug("Failed creating table, retrying", e);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
throw new DatabaseException(e1);
}
} while (tables == null);
if (!tables.contains(LINK_RESOURCE_ALLOCATIONS)) {
databaseAdminService.createTable(LINK_RESOURCE_ALLOCATIONS);
}
if (!tables.contains(INTENT_ALLOCATIONS)) {
databaseAdminService.createTable(INTENT_ALLOCATIONS);
} while (!tableReady);
}
@Activate
public void activate() {
serializer = new KryoSerializer();
createTable(LINK_RESOURCE_ALLOCATIONS);
createTable(INTENT_ALLOCATIONS);
log.info("Started");
}
......
......@@ -23,6 +23,7 @@ import java.util.Map;
import org.onlab.onos.store.serializers.StoreSerializer;
import org.onlab.onos.store.service.DatabaseAdminService;
import org.onlab.onos.store.service.DatabaseException;
import org.onlab.onos.store.service.DatabaseService;
import org.onlab.onos.store.service.VersionedValue;
......@@ -70,9 +71,21 @@ public class CMap<K, V> {
this.tableName = checkNotNull(tableName);
this.serializer = checkNotNull(serializer);
boolean tableReady = false;
do {
try {
if (!dbAdminService.listTables().contains(tableName)) {
dbAdminService.createTable(tableName);
}
tableReady = true;
} catch (DatabaseException e) {
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
throw new DatabaseException(e1);
}
}
} while (!tableReady);
keyCache = CacheBuilder.newBuilder()
.softValues()
......