use of org.apache.accumulo.core.data.NamespaceId in project accumulo by apache.
the class ThriftClientHandler method startMultiScan.
@Override
public InitialMultiScan startMultiScan(TInfo tinfo, TCredentials credentials, Map<TKeyExtent, List<TRange>> tbatch, List<TColumn> tcolumns, List<IterInfo> ssiList, Map<String, Map<String, String>> ssio, List<ByteBuffer> authorizations, boolean waitForWrites, TSamplerConfiguration tSamplerConfig, long batchTimeOut, String contextArg, Map<String, String> executionHints) throws ThriftSecurityException, TSampleNotPresentException {
// find all of the tables that need to be scanned
final HashSet<TableId> tables = new HashSet<>();
for (TKeyExtent keyExtent : tbatch.keySet()) {
tables.add(TableId.of(new String(keyExtent.getTable(), UTF_8)));
}
if (tables.size() != 1) {
throw new IllegalArgumentException("Cannot batch scan over multiple tables");
}
// check if user has permission to the tables
for (TableId tableId : tables) {
NamespaceId namespaceId = getNamespaceId(credentials, tableId);
if (!security.canScan(credentials, tableId, namespaceId, tbatch, tcolumns, ssiList, ssio, authorizations)) {
throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED);
}
}
try {
if (!security.authenticatedUserHasAuthorizations(credentials, authorizations)) {
throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.BAD_AUTHORIZATIONS);
}
} catch (ThriftSecurityException tse) {
log.error("{} is not authorized", credentials.getPrincipal(), tse);
throw tse;
}
// @formatter:off
Map<KeyExtent, List<Range>> batch = tbatch.entrySet().stream().collect(Collectors.toMap(entry -> KeyExtent.fromThrift(entry.getKey()), entry -> entry.getValue().stream().map(Range::new).collect(Collectors.toList())));
// @formatter:on
// This is used to determine which thread pool to use
KeyExtent threadPoolExtent = batch.keySet().iterator().next();
if (waitForWrites) {
writeTracker.waitForWrites(TabletType.type(batch.keySet()));
}
Set<Column> columnSet = tcolumns.isEmpty() ? Collections.emptySet() : new HashSet<>(Collections2.transform(tcolumns, Column::new));
ScanParameters scanParams = new ScanParameters(-1, new Authorizations(authorizations), columnSet, ssiList, ssio, false, SamplerConfigurationImpl.fromThrift(tSamplerConfig), batchTimeOut, contextArg);
final MultiScanSession mss = new MultiScanSession(credentials, threadPoolExtent, batch, scanParams, executionHints);
mss.numTablets = batch.size();
for (List<Range> ranges : batch.values()) {
mss.numRanges += ranges.size();
}
long sid = server.sessionManager.createSession(mss, true);
MultiScanResult result;
try {
result = continueMultiScan(sid, mss);
} finally {
server.sessionManager.unreserveSession(sid);
}
return new InitialMultiScan(sid, result);
}
use of org.apache.accumulo.core.data.NamespaceId in project accumulo by apache.
the class ThriftClientHandler method splitTablet.
@Override
public void splitTablet(TInfo tinfo, TCredentials credentials, TKeyExtent tkeyExtent, ByteBuffer splitPoint) throws NotServingTabletException, ThriftSecurityException {
TableId tableId = TableId.of(new String(ByteBufferUtil.toBytes(tkeyExtent.table)));
NamespaceId namespaceId = getNamespaceId(credentials, tableId);
if (!security.canSplitTablet(credentials, tableId, namespaceId)) {
throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED);
}
KeyExtent keyExtent = KeyExtent.fromThrift(tkeyExtent);
Tablet tablet = server.getOnlineTablet(keyExtent);
if (tablet == null) {
throw new NotServingTabletException(tkeyExtent);
}
if (keyExtent.endRow() == null || !keyExtent.endRow().equals(ByteBufferUtil.toText(splitPoint))) {
try {
if (server.splitTablet(tablet, ByteBufferUtil.toBytes(splitPoint)) == null) {
throw new NotServingTabletException(tkeyExtent);
}
} catch (IOException e) {
log.warn("Failed to split " + keyExtent, e);
throw new RuntimeException(e);
}
}
}
use of org.apache.accumulo.core.data.NamespaceId in project accumulo by apache.
the class CompactRange method call.
@Override
public Repo<Manager> call(final long tid, Manager env) throws Exception {
String zTablePath = Constants.ZROOT + "/" + env.getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID;
ZooReaderWriter zoo = env.getContext().getZooReaderWriter();
byte[] cid;
try {
cid = zoo.mutateExisting(zTablePath, currentValue -> {
String cvs = new String(currentValue, UTF_8);
String[] tokens = cvs.split(",");
long flushID = Long.parseLong(tokens[0]) + 1;
String txidString = String.format("%016x", tid);
for (int i = 1; i < tokens.length; i++) {
if (tokens[i].startsWith(txidString))
// skip self
continue;
log.debug("txidString : {}", txidString);
log.debug("tokens[{}] : {}", i, tokens[i]);
throw new AcceptableThriftTableOperationException(tableId.canonical(), null, TableOperation.COMPACT, TableOperationExceptionType.OTHER, "Another compaction with iterators and/or a compaction strategy is running");
}
StringBuilder encodedIterators = new StringBuilder();
if (config != null) {
Hex hex = new Hex();
encodedIterators.append(",");
encodedIterators.append(txidString);
encodedIterators.append("=");
encodedIterators.append(new String(hex.encode(config), UTF_8));
}
return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8);
});
return new CompactionDriver(Long.parseLong(new String(cid, UTF_8).split(",")[0]), namespaceId, tableId, startRow, endRow);
} catch (NoNodeException nne) {
throw new AcceptableThriftTableOperationException(tableId.canonical(), null, TableOperation.COMPACT, TableOperationExceptionType.NOTFOUND, null);
}
}
use of org.apache.accumulo.core.data.NamespaceId in project accumulo by apache.
the class ThriftClientHandler method update.
@Override
public void update(TInfo tinfo, TCredentials credentials, TKeyExtent tkeyExtent, TMutation tmutation, TDurability tdurability) throws NotServingTabletException, ConstraintViolationException, ThriftSecurityException {
final TableId tableId = TableId.of(new String(tkeyExtent.getTable(), UTF_8));
NamespaceId namespaceId = getNamespaceId(credentials, tableId);
if (!security.canWrite(credentials, tableId, namespaceId)) {
throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED);
}
final KeyExtent keyExtent = KeyExtent.fromThrift(tkeyExtent);
final Tablet tablet = server.getOnlineTablet(KeyExtent.copyOf(keyExtent));
if (tablet == null) {
throw new NotServingTabletException(tkeyExtent);
}
Durability tabletDurability = tablet.getDurability();
if (!keyExtent.isMeta()) {
try {
server.resourceManager.waitUntilCommitsAreEnabled();
} catch (HoldTimeoutException hte) {
// was a failure and it should retry.
throw new NotServingTabletException(tkeyExtent);
}
}
final long opid = writeTracker.startWrite(TabletType.type(keyExtent));
try {
final Mutation mutation = new ServerMutation(tmutation);
final List<Mutation> mutations = Collections.singletonList(mutation);
PreparedMutations prepared;
Span span = TraceUtil.startSpan(this.getClass(), "update::prep");
try (Scope scope = span.makeCurrent()) {
prepared = tablet.prepareMutationsForCommit(new TservConstraintEnv(server.getContext(), security, credentials), mutations);
} catch (Exception e) {
TraceUtil.setException(span, e, true);
throw e;
} finally {
span.end();
}
if (prepared.tabletClosed()) {
throw new NotServingTabletException(tkeyExtent);
} else if (!prepared.getViolators().isEmpty()) {
throw new ConstraintViolationException(prepared.getViolations().asList().stream().map(ConstraintViolationSummary::toThrift).collect(Collectors.toList()));
} else {
CommitSession session = prepared.getCommitSession();
Durability durability = DurabilityImpl.resolveDurabilty(DurabilityImpl.fromThrift(tdurability), tabletDurability);
// Instead of always looping on true, skip completely when durability is NONE.
while (durability != Durability.NONE) {
try {
Span span2 = TraceUtil.startSpan(this.getClass(), "update::wal");
try (Scope scope = span2.makeCurrent()) {
server.logger.log(session, mutation, durability);
} catch (Exception e) {
TraceUtil.setException(span2, e, true);
throw e;
} finally {
span2.end();
}
break;
} catch (IOException ex) {
log.warn("Error writing mutations to log", ex);
}
}
Span span3 = TraceUtil.startSpan(this.getClass(), "update::commit");
try (Scope scope = span3.makeCurrent()) {
session.commit(mutations);
} catch (Exception e) {
TraceUtil.setException(span3, e, true);
throw e;
} finally {
span3.end();
}
}
} finally {
writeTracker.finishWrite(opid);
}
}
use of org.apache.accumulo.core.data.NamespaceId in project accumulo by apache.
the class ThriftClientHandler method startConditionalUpdate.
@Override
public TConditionalSession startConditionalUpdate(TInfo tinfo, TCredentials credentials, List<ByteBuffer> authorizations, String tableIdStr, TDurability tdurabilty, String classLoaderContext) throws ThriftSecurityException, TException {
TableId tableId = TableId.of(tableIdStr);
Authorizations userauths = null;
NamespaceId namespaceId = getNamespaceId(credentials, tableId);
if (!security.canConditionallyUpdate(credentials, tableId, namespaceId)) {
throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED);
}
userauths = security.getUserAuthorizations(credentials);
for (ByteBuffer auth : authorizations) {
if (!userauths.contains(ByteBufferUtil.toBytes(auth))) {
throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.BAD_AUTHORIZATIONS);
}
}
ConditionalSession cs = new ConditionalSession(credentials, new Authorizations(authorizations), tableId, DurabilityImpl.fromThrift(tdurabilty));
long sid = server.sessionManager.createSession(cs, false);
return new TConditionalSession(sid, server.getLockID(), server.sessionManager.getMaxIdleTime());
}
Aggregations