@Override public void delete(String namespace) throws AccumuloException, AccumuloSecurityException, NamespaceNotFoundException, NamespaceNotEmptyException { checkArgument(namespace != null, "namespace is null"); Namespace.ID namespaceId = Namespaces.getNamespaceId(context, namespace); if (namespaceId.equals(Namespace.ID.ACCUMULO) || namespaceId.equals(Namespace.ID.DEFAULT)) { Credentials credentials = context.getCredentials(); log.debug("{} attempted to delete the {} namespace", credentials.getPrincipal(), namespaceId); throw new AccumuloSecurityException(credentials.getPrincipal(), SecurityErrorCode.UNSUPPORTED_OPERATION); } if (Namespaces.getTableIds(context, namespaceId).size() > 0) { throw new NamespaceNotEmptyException(namespaceId.canonicalID(), namespace, null); } List<ByteBuffer> args = Arrays.asList(ByteBuffer.wrap(namespace.getBytes(UTF_8))); Map<String,String> opts = new HashMap<>(); try { doNamespaceFateOperation(FateOperation.NAMESPACE_DELETE, args, opts, namespace); } catch (NamespaceExistsException e) { // should not happen throw new AssertionError(e); } }
Namespace.ID namespaceId = Namespace.ID.of(namespaceIdStr);
/** * Get a Namespace.ID object for the provided canonical string. * * @param canonical * Namespace ID string * @return Namespace.ID object */ public static Namespace.ID of(final String canonical) { try { return cache.get(canonical, () -> new Namespace.ID(canonical)); } catch (ExecutionException e) { throw new AssertionError( "This should never happen: ID constructor should never return null."); } } }
tableId, targetNamespace); zoo.putPersistentData(tables + "/" + tableId + Constants.ZTABLE_NAMESPACE, targetNamespace.getUtf8(), NodeExistsPolicy.SKIP); zoo.putPersistentData(users + "/" + user + "/Namespaces", new byte[0], NodeExistsPolicy.SKIP); perm.grantNamespacePermission(user, Namespace.ID.ACCUMULO.canonicalID(), NamespacePermission.READ); perm.grantNamespacePermission("root", Namespace.ID.ACCUMULO.canonicalID(), NamespacePermission.ALTER_TABLE);
.equals(Namespaces.getNamespaceId(master.getContext(), qualifiedNewTableName.getFirst()))) throw new AcceptableThriftTableOperationException(tableId.canonicalID(), oldTableName, TableOperation.RENAME, TableOperationExceptionType.INVALID_NAME,
private void alterNamespaceProperty(TCredentials c, String namespace, String property, String value, TableOperation op) throws ThriftSecurityException, ThriftTableOperationException { Namespace.ID namespaceId = null; namespaceId = ClientServiceHandler.checkNamespaceId(master.getContext(), namespace, op); if (!master.security.canAlterNamespace(c, namespaceId)) throw new ThriftSecurityException(c.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED); try { if (value == null) { NamespacePropUtil.removeNamespaceProperty(master.getContext(), namespaceId, property); } else { NamespacePropUtil.setNamespaceProperty(master.getContext(), namespaceId, property, value); } } catch (KeeperException.NoNodeException e) { // race condition... namespace no longer exists? This call will throw an exception if the // namespace was deleted: ClientServiceHandler.checkNamespaceId(master.getContext(), namespace, op); log.info("Error altering namespace property", e); throw new ThriftTableOperationException(namespaceId.canonicalID(), namespace, op, TableOperationExceptionType.OTHER, "Problem altering namespaceproperty"); } catch (Exception e) { log.error("Problem altering namespace property", e); throw new ThriftTableOperationException(namespaceId.canonicalID(), namespace, op, TableOperationExceptionType.OTHER, "Problem altering namespace property"); } }
/** * Returns the namespace id for a given table ID. * * @param context * The Accumulo client context * @param tableId * The tableId * @return The namespace id which this table resides in. * @throws IllegalArgumentException * if the table doesn't exist in ZooKeeper */ public static Namespace.ID getNamespaceId(ClientContext context, Table.ID tableId) throws TableNotFoundException { checkArgument(context != null, "instance is null"); checkArgument(tableId != null, "tableId is null"); ZooCache zc = getZooCache(context); byte[] n = zc.get(context.getZooKeeperRoot() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_NAMESPACE); // We might get null out of ZooCache if this tableID doesn't exist if (n == null) { throw new TableNotFoundException(tableId.canonicalID(), null, null); } return Namespace.ID.of(new String(n, UTF_8)); } }
/** * Checks if a user has a namespace permission<br> * This cannot check if a system user has permission. * * @return true if a user exists and has permission; false otherwise */ protected boolean _hasNamespacePermission(String user, Namespace.ID namespace, NamespacePermission permission, boolean useCached) throws ThriftSecurityException { if (permission == null) return false; targetUserExists(user); if (namespace.equals(Namespace.ID.ACCUMULO) && permission.equals(NamespacePermission.READ)) return true; try { if (useCached) return permHandle.hasCachedNamespacePermission(user, namespace.canonicalID(), permission); return permHandle.hasNamespacePermission(user, namespace.canonicalID(), permission); } catch (NamespaceNotFoundException e) { throw new ThriftSecurityException(user, SecurityErrorCode.NAMESPACE_DOESNT_EXIST); } }
public static void prepareNewTableState(IZooReaderWriter zoo, String instanceId, Table.ID tableId, Namespace.ID namespaceId, String tableName, TableState state, NodeExistsPolicy existsPolicy) throws KeeperException, InterruptedException { // state gets created last log.debug("Creating ZooKeeper entries for new table {} (ID: {}) in namespace (ID: {})", tableName, tableId, namespaceId); Pair<String,String> qualifiedTableName = Tables.qualify(tableName); tableName = qualifiedTableName.getSecond(); String zTablePath = Constants.ZROOT + "/" + instanceId + Constants.ZTABLES + "/" + tableId; zoo.putPersistentData(zTablePath, new byte[0], existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_CONF, new byte[0], existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_NAMESPACE, namespaceId.getUtf8(), existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_NAME, tableName.getBytes(UTF_8), existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_FLUSH_ID, ZERO_BYTE, existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_COMPACT_ID, ZERO_BYTE, existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_COMPACT_CANCEL_ID, ZERO_BYTE, existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_STATE, state.name().getBytes(UTF_8), existsPolicy); }
@Override public Repo<Master> call(long tid, Master environment) { // directories are intentionally not created.... this is done because directories should be // unique // because they occupy a different namespace than normal tablet directories... also some clones // may never create files.. therefore there is no need to consume namenode space w/ directories // that are not used... tablet will create directories as needed environment.getTableManager().transitionTableState(cloneInfo.tableId, TableState.ONLINE); Utils.unreserveNamespace(environment, cloneInfo.srcNamespaceId, tid, false); if (!cloneInfo.srcNamespaceId.equals(cloneInfo.namespaceId)) Utils.unreserveNamespace(environment, cloneInfo.namespaceId, tid, false); Utils.unreserveTable(environment, cloneInfo.srcTableId, tid, false); Utils.unreserveTable(environment, cloneInfo.tableId, tid, true); environment.getEventCoordinator().event("Cloned table %s from %s", cloneInfo.tableName, cloneInfo.srcTableId); LoggerFactory.getLogger(FinishCloneTable.class).debug("Cloned table " + cloneInfo.srcTableId + " " + cloneInfo.tableId + " " + cloneInfo.tableName); return null; }
public void deleteNamespace(TCredentials credentials, Namespace.ID namespace) throws ThriftSecurityException { if (!canDeleteNamespace(credentials, namespace)) throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED); try { permHandle.cleanNamespacePermissions(namespace.canonicalID()); } catch (AccumuloSecurityException e) { e.setUser(credentials.getPrincipal()); throw e.asThriftException(); } catch (NamespaceNotFoundException e) { throw new ThriftSecurityException(credentials.getPrincipal(), SecurityErrorCode.NAMESPACE_DOESNT_EXIST); } }
public void revokeNamespacePermission(TCredentials c, String user, Namespace.ID namespace, NamespacePermission permission) throws ThriftSecurityException { if (!canRevokeNamespace(c, namespace)) throw new ThriftSecurityException(c.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED); targetUserExists(user); try { permHandle.revokeNamespacePermission(user, namespace.canonicalID(), permission); log.info("Revoked namespace permission {} for user {} on the namespace {}" + " at the request of user {}", permission, user, namespace, c.getPrincipal()); } catch (AccumuloSecurityException e) { throw e.asThriftException(); } catch (NamespaceNotFoundException e) { throw new ThriftSecurityException(c.getPrincipal(), SecurityErrorCode.NAMESPACE_DOESNT_EXIST); } }
public void grantNamespacePermission(TCredentials c, String user, Namespace.ID namespace, NamespacePermission permission) throws ThriftSecurityException { if (!canGrantNamespace(c, namespace)) throw new ThriftSecurityException(c.getPrincipal(), SecurityErrorCode.PERMISSION_DENIED); targetUserExists(user); try { permHandle.grantNamespacePermission(user, namespace.canonicalID(), permission); log.info("Granted namespace permission {} for user {} on the namespace {}" + " at the request of user {}", permission, user, namespace, c.getPrincipal()); } catch (AccumuloSecurityException e) { throw e.asThriftException(); } catch (NamespaceNotFoundException e) { throw new ThriftSecurityException(c.getPrincipal(), SecurityErrorCode.NAMESPACE_DOESNT_EXIST); } }
private synchronized ZooCachePropertyAccessor getPropCacheAccessor() { if (propCacheAccessor == null) { synchronized (propCaches) { PropCacheKey key = new PropCacheKey(context.getInstanceID(), namespaceId.canonicalID()); ZooCache propCache = propCaches.get(key); if (propCache == null) { propCache = zcf.getZooCache(context.getZooKeepers(), context.getZooKeepersSessionTimeOut(), new NamespaceConfWatcher(context)); propCaches.put(key, propCache); } propCacheAccessor = new ZooCachePropertyAccessor(propCache); } } return propCacheAccessor; }
/** * Look for namespace ID in ZK. Throw NamespaceNotFoundException if not found. */ public static Namespace.ID getNamespaceId(ClientContext context, String namespaceName) throws NamespaceNotFoundException { final ArrayList<Namespace.ID> singleId = new ArrayList<>(1); getAllNamespaces(context, (id, name) -> { if (name.equals(namespaceName)) singleId.add(Namespace.ID.of(id)); }); if (singleId.isEmpty()) throw new NamespaceNotFoundException(null, namespaceName, "getNamespaceId() failed to find namespace"); return singleId.get(0); }
public static long reserveNamespace(Master env, Namespace.ID namespaceId, long id, boolean writeLock, boolean mustExist, TableOperation op) throws Exception { if (getLock(env.getContext(), namespaceId, id, writeLock).tryLock()) { if (mustExist) { IZooReaderWriter zk = env.getContext().getZooReaderWriter(); if (!zk.exists( env.getContext().getZooKeeperRoot() + Constants.ZNAMESPACES + "/" + namespaceId)) throw new AcceptableThriftTableOperationException(namespaceId.canonicalID(), "", op, TableOperationExceptionType.NAMESPACE_NOTFOUND, "Namespace does not exist"); } log.info("namespace {} ({}) locked for {} operation: {}", namespaceId, Long.toHexString(id), (writeLock ? "write" : "read"), op); return 0; } else return 100; }
/** * Look for namespace name in ZK. Throw NamespaceNotFoundException if not found. */ public static String getNamespaceName(ClientContext context, Namespace.ID namespaceId) throws NamespaceNotFoundException { String name; ZooCache zc = context.getZooCache(); byte[] path = zc.get(context.getZooKeeperRoot() + Constants.ZNAMESPACES + "/" + namespaceId.canonicalID() + Constants.ZNAMESPACE_NAME); if (path != null) name = new String(path, UTF_8); else throw new NamespaceNotFoundException(namespaceId.canonicalID(), null, "getNamespaceName() failed to find namespace"); return name; }
@Override public String getReturn() { return namespaceInfo.namespaceId.canonicalID(); }
@Override public Map<String,String> namespaceIdMap() { return Namespaces.getNameToIdMap(context).entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().canonicalID(), (v1, v2) -> { throw new RuntimeException(String.format("Duplicate key for values %s and %s", v1, v2)); }, TreeMap::new)); }