use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class JournalContextTest method pauseBlocksJournalContext.
@Test
public void pauseBlocksJournalContext() throws Exception {
LockResource lock = mMasterContext.getStateLockManager().lockExclusive(StateLockOptions.defaults());
AtomicBoolean journalContextCreated = new AtomicBoolean(false);
Runnable run = () -> {
// new journal contexts should be blocked
try (JournalContext journalContext = mBlockMaster.createJournalContext()) {
journalContextCreated.set(true);
} catch (UnavailableException e) {
throw new RuntimeException("Failed to create journal context", e);
}
};
Thread thread = new Thread(run);
Thread thread2 = new Thread(run);
thread.start();
try {
// since state is paused, new contexts should not be created
CommonUtils.sleepMs(100);
assertFalse(journalContextCreated.get());
// after un-pausing, new journal contexts can be created
lock.close();
thread2.run();
CommonUtils.waitFor("journal context created", journalContextCreated::get, WaitForOptions.defaults().setTimeoutMs(5 * Constants.SECOND_MS).setInterval(10));
} finally {
thread.interrupt();
thread.join();
thread2.interrupt();
thread2.join();
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class JournalContextTest method stateChangeFairness.
@Test
public void stateChangeFairness() throws Exception {
JournalContext journalContext = mBlockMaster.createJournalContext();
AtomicBoolean paused = new AtomicBoolean(false);
ExecutorService service = ExecutorServiceFactories.cachedThreadPool("stateChangeFairness").create();
// create tasks that continually create journal contexts
for (int i = 0; i < 100; i++) {
service.submit(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
mBlockMaster.createJournalContext().close();
} catch (UnavailableException e) {
// ignore
}
}
});
}
// task that attempts to pause the state
service.submit(() -> {
// the pause lock should block
try (LockResource lr = mMasterContext.getStateLockManager().lockExclusive(StateLockOptions.defaults())) {
paused.set(true);
} catch (Exception e) {
throw new IllegalStateException("Failed to acquire state-lock exclusively.");
}
});
try {
// since the journal context is still open, the pause should be blocked
CommonUtils.sleepMs(100);
assertFalse(paused.get());
// after closing the journal context, the pause lock should succeed, even when there are many
// threads creating journal contexts.
journalContext.close();
CommonUtils.waitFor("pause lock to succeed", paused::get, WaitForOptions.defaults().setTimeoutMs(10 * Constants.SECOND_MS).setInterval(10));
} finally {
service.shutdownNow();
service.awaitTermination(5, TimeUnit.SECONDS);
}
assertTrue(paused.get());
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class AlluxioCatalog method completeTransformTable.
/**
* Completes table transformation by updating the layouts of the table's partitions.
*
* @param journalContext the journal context
* @param dbName the database name
* @param tableName the table name
* @param definition the transformation definition
* @param transformedLayouts map from partition spec to the transformed layouts
*/
public void completeTransformTable(JournalContext journalContext, String dbName, String tableName, String definition, Map<String, Layout> transformedLayouts) throws IOException {
try (LockResource l = getDbLock(dbName)) {
// Check existence of table.
getTableInternal(dbName, tableName);
alluxio.proto.journal.Table.CompleteTransformTableEntry entry = alluxio.proto.journal.Table.CompleteTransformTableEntry.newBuilder().setDbName(dbName).setTableName(tableName).setDefinition(definition).putAllTransformedLayouts(Maps.transformValues(transformedLayouts, Layout::toProto)).build();
applyAndJournal(journalContext, Journal.JournalEntry.newBuilder().setCompleteTransformTable(entry).build());
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class AlluxioCatalog method attachDatabase.
/**
* Attaches an udb database to Alluxio catalog.
*
* @param journalContext journal context
* @param udbType the database type
* @param udbConnectionUri the udb connection uri
* @param udbDbName the database name in the udb
* @param dbName the database name in Alluxio
* @param map the configuration
* @param ignoreSyncErrors if true, will ignore syncing errors during the attach
* @return the sync status for the attach
*/
public SyncStatus attachDatabase(JournalContext journalContext, String udbType, String udbConnectionUri, String udbDbName, String dbName, Map<String, String> map, boolean ignoreSyncErrors) throws IOException {
try (LockResource l = getDbLock(dbName)) {
if (mDBs.containsKey(dbName)) {
throw new IOException(String.format("Unable to attach database. Database name %s (type: %s) already exists.", dbName, udbType));
}
applyAndJournal(journalContext, Journal.JournalEntry.newBuilder().setAttachDb(alluxio.proto.journal.Table.AttachDbEntry.newBuilder().setUdbType(udbType).setUdbConnectionUri(udbConnectionUri).setUdbDbName(udbDbName).setDbName(dbName).putAllConfig(map).build()).build());
boolean syncError = false;
try {
SyncStatus status = mDBs.get(dbName).sync(journalContext);
syncError = status.getTablesErrorsCount() > 0;
return status;
} catch (Exception e) {
// Failed to connect to and sync the udb.
syncError = true;
// log the error and stack
LOG.error(String.format("Sync (during attach) failed for db '%s'.", dbName), e);
throw new IOException(String.format("Failed to connect underDb for Alluxio db '%s': %s", dbName, e.getMessage()), e);
} finally {
if (syncError && !ignoreSyncErrors) {
applyAndJournal(journalContext, Journal.JournalEntry.newBuilder().setDetachDb(alluxio.proto.journal.Table.DetachDbEntry.newBuilder().setDbName(dbName).build()).build());
}
}
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class BackupCommandStateLockingIntegrationTest method timeoutWhenStateLockAcquired.
@Test
public void timeoutWhenStateLockAcquired() throws Exception {
// Grab the master state-lock-manager via reflection.
MasterProcess masterProcess = Whitebox.getInternalState(mLocalAlluxioCluster.getLocalAlluxioMaster(), "mMasterProcess");
MasterContext masterCtx = Whitebox.getInternalState(masterProcess, "mContext");
StateLockManager stateLockManager = masterCtx.getStateLockManager();
// Lock the state-change lock on the master before initiating the backup.
try (LockResource lr = stateLockManager.lockExclusive(StateLockOptions.defaults())) {
// Prepare for a backup.
Path dir = Paths.get(ServerConfiguration.getString(PropertyKey.MASTER_BACKUP_DIRECTORY));
Files.createDirectories(dir);
assertEquals(0, Files.list(dir).count());
// Initiate backup. It should fail.
int errCode = mFsAdminShell.run("backup");
assertTrue(mOutput.toString().contains(ExceptionMessage.STATE_LOCK_TIMED_OUT.getMessage(3000)));
assertNotEquals(0, errCode);
}
}
Aggregations