use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.
the class ZooReaderWriterTest method testMutateWithRetryOnSetData.
@Test
public void testMutateWithRetryOnSetData() throws Exception {
final String path = "/foo";
final byte[] value = new byte[] { 0 };
final List<ACL> acls = Collections.emptyList();
final byte[] mutatedBytes = new byte[] { 1 };
Mutator mutator = new Mutator() {
@Override
public byte[] mutate(byte[] currentValue) throws Exception {
return mutatedBytes;
}
};
Method getDataMethod = ZooReaderWriter.class.getMethod("getData", String.class, boolean.class, Stat.class);
zrw = EasyMock.createMockBuilder(ZooReaderWriter.class).addMockedMethods("getRetryFactory", "getZooKeeper").addMockedMethod(getDataMethod).createMock();
EasyMock.expect(zrw.getRetryFactory()).andReturn(retryFactory).anyTimes();
EasyMock.expect(zrw.getZooKeeper()).andReturn(zk).anyTimes();
Stat stat = new Stat();
zk.create(path, value, acls, CreateMode.PERSISTENT);
EasyMock.expectLastCall().andThrow(new NodeExistsException()).once();
EasyMock.expect(zrw.getData(path, false, stat)).andReturn(new byte[] { 3 }).times(2);
// BadVersionException should retry
EasyMock.expect(zk.setData(path, mutatedBytes, 0)).andThrow(new ConnectionLossException());
EasyMock.expect(retry.canRetry()).andReturn(true);
retry.useRetry();
EasyMock.expectLastCall();
retry.waitForNextAttempt();
EasyMock.expectLastCall();
// Let 2nd setData succeed
EasyMock.expect(zk.setData(path, mutatedBytes, 0)).andReturn(null);
EasyMock.replay(zk, zrw, retryFactory, retry);
Assert.assertArrayEquals(new byte[] { 1 }, zrw.mutate(path, value, acls, mutator));
EasyMock.verify(zk, zrw, retryFactory, retry);
}
use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.
the class MasterClientServiceHandler method initiateFlush.
@Override
public long initiateFlush(TInfo tinfo, TCredentials c, String tableIdStr) throws ThriftSecurityException, ThriftTableOperationException {
Table.ID tableId = Table.ID.of(tableIdStr);
Namespace.ID namespaceId = getNamespaceIdFromTableId(TableOperation.FLUSH, tableId);
master.security.canFlush(c, tableId, namespaceId);
String zTablePath = Constants.ZROOT + "/" + master.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_FLUSH_ID;
IZooReaderWriter zoo = ZooReaderWriter.getInstance();
byte[] fid;
try {
fid = zoo.mutate(zTablePath, null, null, new Mutator() {
@Override
public byte[] mutate(byte[] currentValue) throws Exception {
long flushID = Long.parseLong(new String(currentValue));
flushID++;
return ("" + flushID).getBytes();
}
});
} catch (NoNodeException nne) {
throw new ThriftTableOperationException(tableId.canonicalID(), null, TableOperation.FLUSH, TableOperationExceptionType.NOTFOUND, null);
} catch (Exception e) {
Master.log.warn("{}", e.getMessage(), e);
throw new ThriftTableOperationException(tableId.canonicalID(), null, TableOperation.FLUSH, TableOperationExceptionType.OTHER, null);
}
return Long.parseLong(new String(fid));
}
use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.
the class CancelCompactions method call.
@Override
public Repo<Master> call(long tid, Master environment) throws Exception {
String zCompactID = Constants.ZROOT + "/" + environment.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID;
String zCancelID = Constants.ZROOT + "/" + environment.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_CANCEL_ID;
IZooReaderWriter zoo = ZooReaderWriter.getInstance();
byte[] currentValue = zoo.getData(zCompactID, null);
String cvs = new String(currentValue, UTF_8);
String[] tokens = cvs.split(",");
final long flushID = Long.parseLong(tokens[0]);
zoo.mutate(zCancelID, null, null, new Mutator() {
@Override
public byte[] mutate(byte[] currentValue) throws Exception {
long cid = Long.parseLong(new String(currentValue, UTF_8));
if (cid < flushID)
return Long.toString(flushID).getBytes(UTF_8);
else
return Long.toString(cid).getBytes(UTF_8);
}
});
return new FinishCancelCompaction(namespaceId, tableId);
}
use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.
the class RenameTable method call.
@Override
public Repo<Master> call(long tid, Master master) throws Exception {
Instance instance = master.getInstance();
Pair<String, String> qualifiedOldTableName = Tables.qualify(oldTableName);
Pair<String, String> qualifiedNewTableName = Tables.qualify(newTableName);
// ensure no attempt is made to rename across namespaces
if (newTableName.contains(".") && !namespaceId.equals(Namespaces.getNamespaceId(instance, qualifiedNewTableName.getFirst())))
throw new AcceptableThriftTableOperationException(tableId.canonicalID(), oldTableName, TableOperation.RENAME, TableOperationExceptionType.INVALID_NAME, "Namespace in new table name does not match the old table name");
IZooReaderWriter zoo = ZooReaderWriter.getInstance();
Utils.tableNameLock.lock();
try {
Utils.checkTableDoesNotExist(instance, newTableName, tableId, TableOperation.RENAME);
final String newName = qualifiedNewTableName.getSecond();
final String oldName = qualifiedOldTableName.getSecond();
final String tap = ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_NAME;
zoo.mutate(tap, null, null, new Mutator() {
@Override
public byte[] mutate(byte[] current) throws Exception {
final String currentName = new String(current, UTF_8);
if (currentName.equals(newName))
// assume in this case the operation is running again, so we are done
return null;
if (!currentName.equals(oldName)) {
throw new AcceptableThriftTableOperationException(null, oldTableName, TableOperation.RENAME, TableOperationExceptionType.NOTFOUND, "Name changed while processing");
}
return newName.getBytes(UTF_8);
}
});
Tables.clearCache(instance);
} finally {
Utils.tableNameLock.unlock();
Utils.unreserveTable(tableId, tid, true);
Utils.unreserveNamespace(namespaceId, tid, false);
}
LoggerFactory.getLogger(RenameTable.class).debug("Renamed table {} {} {}", tableId, oldTableName, newTableName);
return null;
}
Aggregations