org.apache.accumulo.core.client.ConditionalWriterConfig Java Examples

The following examples show how to use org.apache.accumulo.core.client.ConditionalWriterConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SharedResources.java    From fluo with Apache License 2.0 5 votes vote down vote up
public SharedResources(Environment env) throws TableNotFoundException {
  this.env = env;
  curator = CuratorUtil.newAppCurator(env.getConfiguration());
  curator.start();

  int numTservers = env.getAccumuloClient().instanceOperations().getTabletServers().size();
  int numBWThreads = FluoConfigurationImpl.getNumBWThreads(env.getConfiguration(), numTservers);
  bw = env.getAccumuloClient().createBatchWriter(env.getTable(),
      new BatchWriterConfig().setMaxWriteThreads(numBWThreads));
  sbw = new SharedBatchWriter(bw);

  int numCWThreads = FluoConfigurationImpl.getNumCWThreads(env.getConfiguration(), numTservers);
  cw = env.getAccumuloClient().createConditionalWriter(env.getTable(),
      new ConditionalWriterConfig().setAuthorizations(env.getAuthorizations())
          .setMaxWriteThreads(numCWThreads));
  bulkCw = env.getAccumuloClient().createConditionalWriter(env.getTable(),
      new ConditionalWriterConfig().setAuthorizations(env.getAuthorizations())
          .setMaxWriteThreads(numCWThreads));

  txInfoCache = new TxInfoCache(env);
  visCache = new VisibilityCache(env.getConfiguration());
  metricRegistry = new MetricRegistry();

  int commitThreads = env.getConfiguration().getInt(FluoConfigurationImpl.ASYNC_COMMIT_THREADS,
      FluoConfigurationImpl.ASYNC_COMMIT_THREADS_DEFAULT);
  asyncCommitExecutor = FluoExecutors.newFixedThreadPool(commitThreads, "async-commits");

  commitThreads = env.getConfiguration().getInt(FluoConfigurationImpl.SYNC_COMMIT_THREADS,
      FluoConfigurationImpl.SYNC_COMMIT_THREADS_DEFAULT);
  syncCommitExecutor = FluoExecutors.newFixedThreadPool(commitThreads, "sync-commits");

  acw = new AsyncConditionalWriter(env, cw);
  bulkAcw = new AsyncConditionalWriter(env, bulkCw);
}
 
Example #2
Source File: RebuildingScannerTestHelper.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public ConditionalWriter createConditionalWriter(String s, ConditionalWriterConfig conditionalWriterConfig) throws TableNotFoundException {
    return delegate.createConditionalWriter(s, conditionalWriterConfig);
}
 
Example #3
Source File: ARS.java    From accumulo-examples with Apache License 2.0 4 votes vote down vote up
public void cancel(String what, String when, String who) throws Exception {

    String row = what + ":" + when;

    // Even though this method is only deleting a column, its important to use a conditional writer.
    // By updating the seq # when deleting a reservation, it
    // will cause any concurrent reservations to retry. If this delete were done using a batch
    // writer, then a concurrent reservation could report WAIT_LISTED
    // when it actually got the reservation.

    // its important to use an isolated scanner so that only whole mutations are seen
    try (
        ConditionalWriter cwriter = client.createConditionalWriter(rTable,
            new ConditionalWriterConfig());
        Scanner scanner = new IsolatedScanner(client.createScanner(rTable, Authorizations.EMPTY))) {
      while (true) {
        scanner.setRange(new Range(row));

        int seq = -1;
        String reservation = null;

        for (Entry<Key,Value> entry : scanner) {
          String cf = entry.getKey().getColumnFamilyData().toString();
          String cq = entry.getKey().getColumnQualifierData().toString();
          String val = entry.getValue().toString();

          // EXCERCISE avoid linear scan

          if (cf.equals("tx") && cq.equals("seq")) {
            seq = Integer.parseInt(val);
          } else if (cf.equals("res") && val.equals(who)) {
            reservation = cq;
          }
        }

        if (reservation != null) {
          ConditionalMutation update = new ConditionalMutation(row,
              new Condition("tx", "seq").setValue(seq + ""));
          update.putDelete("res", reservation);
          update.put("tx", "seq", (seq + 1) + "");

          Status status = cwriter.write(update).getStatus();
          switch (status) {
            case ACCEPTED:
              // successfully canceled reservation
              return;
            case REJECTED:
            case UNKNOWN:
              // retry
              // EXCERCISE exponential back-off could be used here
              break;
            default:
              throw new RuntimeException("Unexpected status " + status);
          }

        } else {
          // not reserved, nothing to do
          break;
        }

      }
    }
  }
 
Example #4
Source File: AccumuloRyaInstanceDetailsRepository.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void update(final RyaDetails oldDetails, final RyaDetails newDetails)
        throws NotInitializedException, ConcurrentUpdateException, RyaDetailsRepositoryException {
    // Preconditions.
    requireNonNull(oldDetails);
    requireNonNull(newDetails);

    if(!newDetails.getRyaInstanceName().equals( instanceName )) {
        throw new RyaDetailsRepositoryException("The instance name that was in the provided 'newDetails' does not match " +
                "the instance name that this repository is connected to. Make sure you're connected to the" +
                "correct Rya instance.");
    }

    if(!isInitialized()) {
        throw new NotInitializedException("Could not update the details for the Rya instanced named '" +
                instanceName + "' because it has not been initialized yet.");
    }

    // Use a conditional writer so that we can detect when the old details
    // are no longer the currently stored ones.
    ConditionalWriter writer = null;
    try {
        // Setup the condition that ensures the details have not changed since the edits were made.
        final byte[] oldDetailsBytes = serializer.serialize(oldDetails);
        final Condition condition = new Condition(COL_FAMILY, COL_QUALIFIER);
        condition.setValue( oldDetailsBytes );

        // Create the mutation that only performs the update if the details haven't changed.
        final ConditionalMutation mutation = new ConditionalMutation(ROW_ID);
        mutation.addCondition( condition );
        final byte[] newDetailsBytes = serializer.serialize(newDetails);
        mutation.put(COL_FAMILY, COL_QUALIFIER, new Value(newDetailsBytes));

        // Do the write.
        writer = connector.createConditionalWriter(detailsTableName, new ConditionalWriterConfig());
        final Result result = writer.write(mutation);
        switch(result.getStatus()) {
            case REJECTED:
            case VIOLATED:
                throw new ConcurrentUpdateException("Could not update the details for the Rya instance named '" +
                        instanceName + "' because the old value is out of date.");
            case UNKNOWN:
            case INVISIBLE_VISIBILITY:
                throw new RyaDetailsRepositoryException("Could not update the details for the Rya instance named '" + instanceName + "'.");
        }
    } catch (final TableNotFoundException | AccumuloException | AccumuloSecurityException e) {
        throw new RyaDetailsRepositoryException("Could not update the details for the Rya instance named '" + instanceName + "'.");
    } finally {
        if(writer != null) {
            writer.close();
        }
    }
}
 
Example #5
Source File: PcjTables.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Update the cardinality of a PCJ by a {@code delta}.
 *
 * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
 * @param pcjTableName - The name of the PCJ table that will have its cardinality updated. (not null)
 * @param delta - How much the cardinality will change.
 * @throws PCJStorageException The cardinality could not be updated.
 */
private void updateCardinality(final Connector accumuloConn, final String pcjTableName, final long delta) throws PCJStorageException {
    checkNotNull(accumuloConn);
    checkNotNull(pcjTableName);

    ConditionalWriter conditionalWriter = null;
    try {
        conditionalWriter = accumuloConn.createConditionalWriter(pcjTableName, new ConditionalWriterConfig());

        boolean updated = false;
        while (!updated) {
            // Write the conditional update request to Accumulo.
            final long cardinality = getPcjMetadata(accumuloConn,pcjTableName).getCardinality();
            final ConditionalMutation mutation = makeUpdateCardinalityMutation(cardinality, delta);
            final ConditionalWriter.Result result = conditionalWriter.write(mutation);

            // Interpret the result.
            switch (result.getStatus()) {
            case ACCEPTED:
                updated = true;
                break;
            case REJECTED:
                break;
            case UNKNOWN:
                // We do not know if the mutation succeeded. At best, we
                // can hope the metadata hasn't been updated
                // since we originally fetched it and try again.
                // Otherwise, continue forwards as if it worked. It's
                // okay if this number is slightly off.
                final long newCardinality = getPcjMetadata(accumuloConn,pcjTableName).getCardinality();
                if (newCardinality != cardinality) {
                    updated = true;
                }
                break;
            case VIOLATED:
                throw new PCJStorageException("The cardinality could not be updated because the commit violated a table constraint.");
            case INVISIBLE_VISIBILITY:
                throw new PCJStorageException("The condition contains a visibility the updater can not satisfy.");
            }
        }
    } catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
        throw new PCJStorageException("Could not update the cardinality value of the PCJ Table named: " + pcjTableName, e);
    } finally {
        if (conditionalWriter != null) {
            conditionalWriter.close();
        }
    }
}