Search in sources :

Example 6 with Mutator

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);
}
Also used : Stat(org.apache.zookeeper.data.Stat) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ACL(org.apache.zookeeper.data.ACL) Method(java.lang.reflect.Method) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) Test(org.junit.Test)

Example 7 with Mutator

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));
}
Also used : MetadataTable(org.apache.accumulo.core.metadata.MetadataTable) RootTable(org.apache.accumulo.core.metadata.RootTable) Table(org.apache.accumulo.core.client.impl.Table) ReplicationTable(org.apache.accumulo.core.replication.ReplicationTable) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) IZooReaderWriter(org.apache.accumulo.fate.zookeeper.IZooReaderWriter) ThriftTableOperationException(org.apache.accumulo.core.client.impl.thrift.ThriftTableOperationException) Namespace(org.apache.accumulo.core.client.impl.Namespace) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ThriftSecurityException(org.apache.accumulo.core.client.impl.thrift.ThriftSecurityException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) TabletDeletedException(org.apache.accumulo.server.util.TabletIterator.TabletDeletedException) KeeperException(org.apache.zookeeper.KeeperException) TException(org.apache.thrift.TException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ThriftTableOperationException(org.apache.accumulo.core.client.impl.thrift.ThriftTableOperationException)

Example 8 with Mutator

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);
}
Also used : Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) IZooReaderWriter(org.apache.accumulo.fate.zookeeper.IZooReaderWriter)

Example 9 with Mutator

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;
}
Also used : Instance(org.apache.accumulo.core.client.Instance) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) IZooReaderWriter(org.apache.accumulo.fate.zookeeper.IZooReaderWriter) AcceptableThriftTableOperationException(org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException) NamespaceNotFoundException(org.apache.accumulo.core.client.NamespaceNotFoundException) AcceptableThriftTableOperationException(org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException)

Aggregations

Mutator (org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator)9 IZooReaderWriter (org.apache.accumulo.fate.zookeeper.IZooReaderWriter)6 AcceptableThriftTableOperationException (org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException)4 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)3 ACL (org.apache.zookeeper.data.ACL)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 Instance (org.apache.accumulo.core.client.Instance)2 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)2 Stat (org.apache.zookeeper.data.Stat)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 AccumuloException (org.apache.accumulo.core.client.AccumuloException)1 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)1 NamespaceNotFoundException (org.apache.accumulo.core.client.NamespaceNotFoundException)1 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)1 Namespace (org.apache.accumulo.core.client.impl.Namespace)1 Table (org.apache.accumulo.core.client.impl.Table)1 ThriftSecurityException (org.apache.accumulo.core.client.impl.thrift.ThriftSecurityException)1 ThriftTableOperationException (org.apache.accumulo.core.client.impl.thrift.ThriftTableOperationException)1 MetadataTable (org.apache.accumulo.core.metadata.MetadataTable)1