Java Code Examples for org.apache.hadoop.hbase.client.Table#coprocessorService()

The following examples show how to use org.apache.hadoop.hbase.client.Table#coprocessorService() . 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: RefreshHFilesClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
public void refreshHFiles(final Table table) throws Throwable {
  final RefreshHFilesProtos.RefreshHFilesRequest request =
          RefreshHFilesProtos.RefreshHFilesRequest.getDefaultInstance();
  table.coprocessorService(RefreshHFilesProtos.RefreshHFilesService.class,
          HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW,
          new Batch.Call<RefreshHFilesProtos.RefreshHFilesService,
                  RefreshHFilesProtos.RefreshHFilesResponse>() {
      @Override
      public RefreshHFilesProtos.RefreshHFilesResponse call(
            RefreshHFilesProtos.RefreshHFilesService refreshHFilesService)
            throws IOException {
        ServerRpcController controller = new ServerRpcController();
        BlockingRpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> rpcCallback =
              new BlockingRpcCallback<>();
        refreshHFilesService.refreshHFiles(controller, request, rpcCallback);

        if (controller.failedOnException()) {
          throw controller.getFailedOn();
        }

        return rpcCallback.get();
      }
    });
  LOG.debug("Done refreshing HFiles");
}
 
Example 2
Source File: ClientTokenUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Obtain and return an authentication token for the current user.
 * @param conn The HBase cluster connection
 * @throws IOException if a remote error or serialization problem occurs.
 * @return the authentication token instance
 */
@InterfaceAudience.Private
static Token<AuthenticationTokenIdentifier> obtainToken(
    Connection conn) throws IOException {
  Table meta = null;
  try {
    injectFault();

    meta = conn.getTable(TableName.META_TABLE_NAME);
    CoprocessorRpcChannel rpcChannel = meta.coprocessorService(
            HConstants.EMPTY_START_ROW);
    AuthenticationProtos.AuthenticationService.BlockingInterface service =
        AuthenticationProtos.AuthenticationService.newBlockingStub(rpcChannel);
    AuthenticationProtos.GetAuthenticationTokenResponse response =
            service.getAuthenticationToken(null,
        AuthenticationProtos.GetAuthenticationTokenRequest.getDefaultInstance());

    return toToken(response.getToken());
  } catch (ServiceException se) {
    throw ProtobufUtil.handleRemoteException(se);
  } finally {
    if (meta != null) {
      meta.close();
    }
  }
}
 
Example 3
Source File: TestUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static void clearMetaDataCache(Connection conn) throws Throwable {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    Table htable = pconn.getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
    htable.coprocessorService(MetaDataService.class, HConstants.EMPTY_START_ROW,
        HConstants.EMPTY_END_ROW, new Batch.Call<MetaDataService, ClearCacheResponse>() {
            @Override
            public ClearCacheResponse call(MetaDataService instance) throws IOException {
                ServerRpcController controller = new ServerRpcController();
                BlockingRpcCallback<ClearCacheResponse> rpcCallback =
                        new BlockingRpcCallback<ClearCacheResponse>();
                ClearCacheRequest.Builder builder = ClearCacheRequest.newBuilder();
                instance.clearCache(controller, builder.build(), rpcCallback);
                if(controller.getFailedOn() != null) {
                    throw controller.getFailedOn();
                }
                return rpcCallback.get(); 
            }
          });
}
 
Example 4
Source File: TestServerCustomProtocol.java    From hbase with Apache License 2.0 6 votes vote down vote up
private Map<byte [], String> noop(final Table table, final byte [] start, final byte [] end)
        throws ServiceException, Throwable {
  return table.coprocessorService(PingService.class, start, end,
      new Batch.Call<PingService, String>() {
        @Override
        public String call(PingService instance) throws IOException {
          CoprocessorRpcUtils.BlockingRpcCallback<NoopResponse> rpcCallback =
            new CoprocessorRpcUtils.BlockingRpcCallback<>();
          NoopRequest.Builder builder = NoopRequest.newBuilder();
          instance.noop(null, builder.build(), rpcCallback);
          rpcCallback.get();
          // Looks like null is expected when void.  That is what the test below is looking for
          return null;
        }
      });
}
 
Example 5
Source File: TestServerCustomProtocol.java    From hbase with Apache License 2.0 6 votes vote down vote up
private Map<byte [], String> compoundOfHelloAndPing(final Table table, final byte [] start,
        final byte [] end) throws ServiceException, Throwable {
  return table.coprocessorService(PingService.class,
      start, end,
      new Batch.Call<PingService, String>() {
        @Override
        public String call(PingService instance) throws IOException {
          CoprocessorRpcUtils.BlockingRpcCallback<HelloResponse> rpcCallback =
            new CoprocessorRpcUtils.BlockingRpcCallback<>();
          HelloRequest.Builder builder = HelloRequest.newBuilder();
          // Call ping on same instance.  Use result calling hello on same instance.
          builder.setName(doPing(instance));
          instance.hello(null, builder.build(), rpcCallback);
          HelloResponse r = rpcCallback.get();
          return r != null && r.hasResponse()? r.getResponse(): null;
        }
      });
}
 
Example 6
Source File: TestCoprocessorEndpoint.java    From hbase with Apache License 2.0 6 votes vote down vote up
private Map<byte [], Long> sum(final Table table, final byte [] family,
        final byte [] qualifier, final byte [] start, final byte [] end)
        throws ServiceException, Throwable {
  return table.coprocessorService(ColumnAggregationProtos.ColumnAggregationService.class,
      start, end,
    new Batch.Call<ColumnAggregationProtos.ColumnAggregationService, Long>() {
      @Override
      public Long call(ColumnAggregationProtos.ColumnAggregationService instance)
        throws IOException {
        CoprocessorRpcUtils.BlockingRpcCallback<ColumnAggregationProtos.SumResponse> rpcCallback =
            new CoprocessorRpcUtils.BlockingRpcCallback<>();
        ColumnAggregationProtos.SumRequest.Builder builder =
          ColumnAggregationProtos.SumRequest.newBuilder();
        builder.setFamily(UnsafeByteOperations.unsafeWrap(family));
        if (qualifier != null && qualifier.length > 0) {
          builder.setQualifier(UnsafeByteOperations.unsafeWrap(qualifier));
        }
        instance.sum(null, builder.build(), rpcCallback);
        return rpcCallback.get().getSum();
      }
    });
}
 
Example 7
Source File: TestRowProcessorEndpoint.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void swapRows(Table table) throws Throwable {
  CoprocessorRpcChannel channel = table.coprocessorService(ROW);
  RowProcessorEndpoint.RowSwapProcessor processor =
      new RowProcessorEndpoint.RowSwapProcessor(ROW, ROW2);
  RowProcessorService.BlockingInterface service =
      RowProcessorService.newBlockingStub(channel);
  ProcessRequest request = RowProcessorClient.getRowProcessorPB(processor);
  service.process(null, request);
}
 
Example 8
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static MetaDataMutationResult updateIndexState(byte[] indexTableKey, long minTimeStamp,
        Table metaTable, PIndexState newState) throws Throwable {
    // Mimic the Put that gets generated by the client on an update of the index state
    Put put = new Put(indexTableKey);
    put.addColumn(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.INDEX_STATE_BYTES,
            newState.getSerializedBytes());
    put.addColumn(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.INDEX_DISABLE_TIMESTAMP_BYTES,
            PLong.INSTANCE.toBytes(minTimeStamp));
    put.addColumn(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.ASYNC_REBUILD_TIMESTAMP_BYTES,
            PLong.INSTANCE.toBytes(0));
    final List<Mutation> tableMetadata = Collections.<Mutation> singletonList(put);

    final Map<byte[], MetaDataResponse> results = metaTable.coprocessorService(MetaDataService.class, indexTableKey,
            indexTableKey, new Batch.Call<MetaDataService, MetaDataResponse>() {
                @Override
                public MetaDataResponse call(MetaDataService instance) throws IOException {
                    ServerRpcController controller = new ServerRpcController();
                    BlockingRpcCallback<MetaDataResponse> rpcCallback = new BlockingRpcCallback<MetaDataResponse>();
                    UpdateIndexStateRequest.Builder builder = UpdateIndexStateRequest.newBuilder();
                    for (Mutation m : tableMetadata) {
                        MutationProto mp = ProtobufUtil.toProto(m);
                        builder.addTableMetadataMutations(mp.toByteString());
                    }
                    builder.setClientVersion(VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION, PHOENIX_MINOR_VERSION, PHOENIX_PATCH_NUMBER));
                    instance.updateIndexState(controller, builder.build(), rpcCallback);
                    if (controller.getFailedOn() != null) { throw controller.getFailedOn(); }
                    return rpcCallback.get();
                }
            });
    if (results.isEmpty()) { throw new IOException("Didn't get expected result size"); }
    MetaDataResponse tmpResponse = results.values().iterator().next();
    return MetaDataMutationResult.constructFromProto(tmpResponse);
}
 
Example 9
Source File: TestServerCustomProtocol.java    From hbase with Apache License 2.0 5 votes vote down vote up
private Map<byte [], String> ping(final Table table, final byte [] start, final byte [] end)
        throws ServiceException, Throwable {
  return table.coprocessorService(PingService.class, start, end,
    new Batch.Call<PingService, String>() {
      @Override
      public String call(PingService instance) throws IOException {
        return doPing(instance);
      }
    });
}
 
Example 10
Source File: TestRowProcessorEndpoint.java    From hbase with Apache License 2.0 5 votes vote down vote up
private int incrementCounter(Table table) throws Throwable {
  CoprocessorRpcChannel channel = table.coprocessorService(ROW);
  RowProcessorEndpoint.IncrementCounterProcessor processor =
      new RowProcessorEndpoint.IncrementCounterProcessor(ROW);
  RowProcessorService.BlockingInterface service =
      RowProcessorService.newBlockingStub(channel);
  ProcessRequest request = RowProcessorClient.getRowProcessorPB(processor);
  ProcessResponse protoResult = service.process(null, request);
  IncCounterProcessorResponse response = IncCounterProcessorResponse
      .parseFrom(protoResult.getRowProcessorResult());
  Integer result = response.getResponse();
  return result;
}
 
Example 11
Source File: AggregationClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * It gives the row count, by summing up the individual results obtained from
 * regions. In case the qualifier is null, FirstKeyValueFilter is used to
 * optimised the operation. In case qualifier is provided, I can't use the
 * filter as it may set the flag to skip to next row, but the value read is
 * not of the given filter: in this case, this particular row will not be
 * counted ==&gt; an error.
 * @param table table to scan.
 * @param ci the user's ColumnInterpreter implementation
 * @param scan the HBase scan object to use to read data from HBase
 * @return &lt;R, S&gt;
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
public <R, S, P extends Message, Q extends Message, T extends Message>
  long rowCount(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan)
        throws Throwable {
  final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, true);
  class RowNumCallback implements Batch.Callback<Long> {
    private final AtomicLong rowCountL = new AtomicLong(0);

    public long getRowNumCount() {
      return rowCountL.get();
    }

    @Override
    public void update(byte[] region, byte[] row, Long result) {
      rowCountL.addAndGet(result.longValue());
    }
  }

  RowNumCallback rowNum = new RowNumCallback();
  table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(),
      new Batch.Call<AggregateService, Long>() {
        @Override
        public Long call(AggregateService instance) throws IOException {
          RpcController controller = new AggregationClientRpcController();
          CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback =
              new CoprocessorRpcUtils.BlockingRpcCallback<>();
          instance.getRowNum(controller, requestArg, rpcCallback);
          AggregateResponse response = rpcCallback.get();
          if (controller.failed()) {
            throw new IOException(controller.errorText());
          }
          byte[] bytes = getBytesFromResponse(response.getFirstPart(0));
          ByteBuffer bb = ByteBuffer.allocate(8).put(bytes);
          bb.rewind();
          return bb.getLong();
        }
      }, rowNum);
  return rowNum.getRowNumCount();
}
 
Example 12
Source File: AggregationClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * It gives the maximum value of a column for a given column family for the
 * given range. In case qualifier is null, a max of all values for the given
 * family is returned.
 * @param table table to scan.
 * @param ci the user's ColumnInterpreter implementation
 * @param scan the HBase scan object to use to read data from HBase
 * @return max val &lt;&gt;
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
public <R, S, P extends Message, Q extends Message, T extends Message>
  R max(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan)
        throws Throwable {
  final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
  class MaxCallBack implements Batch.Callback<R> {
    R max = null;

    R getMax() {
      return max;
    }

    @Override
    public synchronized void update(byte[] region, byte[] row, R result) {
      max = (max == null || (result != null && ci.compare(max, result) < 0)) ? result : max;
    }
  }
  MaxCallBack aMaxCallBack = new MaxCallBack();
  table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(),
      new Batch.Call<AggregateService, R>() {
        @Override
        public R call(AggregateService instance) throws IOException {
          RpcController controller = new AggregationClientRpcController();
          CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback =
              new CoprocessorRpcUtils.BlockingRpcCallback<>();
          instance.getMax(controller, requestArg, rpcCallback);
          AggregateResponse response = rpcCallback.get();
          if (controller.failed()) {
            throw new IOException(controller.errorText());
          }
          if (response.getFirstPartCount() > 0) {
            ByteString b = response.getFirstPart(0);
            Q q = getParsedGenericInstance(ci.getClass(), 3, b);
            return ci.getCellValueFromProto(q);
          }
          return null;
        }
      }, aMaxCallBack);
  return aMaxCallBack.getMax();
}
 
Example 13
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Performs an atomic multi-mutate operation against the given table. Used by the likes of merge
 * and split as these want to make atomic mutations across multiple rows.
 * @throws IOException even if we encounter a RuntimeException, we'll still wrap it in an IOE.
 */
@VisibleForTesting
static void multiMutate(final Table table, byte[] row, final List<Mutation> mutations)
  throws IOException {
  debugLogMutations(mutations);
  Batch.Call<MultiRowMutationService, MutateRowsResponse> callable = instance -> {
    MutateRowsRequest.Builder builder = MutateRowsRequest.newBuilder();
    for (Mutation mutation : mutations) {
      if (mutation instanceof Put) {
        builder.addMutationRequest(
          ProtobufUtil.toMutation(ClientProtos.MutationProto.MutationType.PUT, mutation));
      } else if (mutation instanceof Delete) {
        builder.addMutationRequest(
          ProtobufUtil.toMutation(ClientProtos.MutationProto.MutationType.DELETE, mutation));
      } else {
        throw new DoNotRetryIOException(
          "multi in MetaEditor doesn't support " + mutation.getClass().getName());
      }
    }
    ServerRpcController controller = new ServerRpcController();
    CoprocessorRpcUtils.BlockingRpcCallback<MutateRowsResponse> rpcCallback =
      new CoprocessorRpcUtils.BlockingRpcCallback<>();
    instance.mutateRows(controller, builder.build(), rpcCallback);
    MutateRowsResponse resp = rpcCallback.get();
    if (controller.failedOnException()) {
      throw controller.getFailedOn();
    }
    return resp;
  };
  try {
    table.coprocessorService(MultiRowMutationService.class, row, row, callable);
  } catch (Throwable e) {
    // Throw if an IOE else wrap in an IOE EVEN IF IT IS a RuntimeException (e.g.
    // a RejectedExecutionException because the hosting exception is shutting down.
    // This is old behavior worth reexamining. Procedures doing merge or split
    // currently don't handle RuntimeExceptions coming up out of meta table edits.
    // Would have to work on this at least. See HBASE-23904.
    Throwables.throwIfInstanceOf(e, IOException.class);
    throw new IOException(e);
  }
}
 
Example 14
Source File: ServerCacheClient.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public boolean addServerCache(Table htable, byte[] key, final PTable cacheUsingTable, final byte[] cacheId,
        final ImmutableBytesWritable cachePtr, final ServerCacheFactory cacheFactory, final byte[] txState, final boolean usePersistentCache)
        throws Exception {
    byte[] keyInRegion = getKeyInRegion(key);
    final Map<byte[], AddServerCacheResponse> results;

    AddServerCacheRequest.Builder builder = AddServerCacheRequest.newBuilder();
    final byte[] tenantIdBytes;
    if (cacheUsingTable.isMultiTenant()) {
        try {
            tenantIdBytes = connection.getTenantId() == null ? null
                    : ScanUtil.getTenantIdBytes(cacheUsingTable.getRowKeySchema(),
                    cacheUsingTable.getBucketNum() != null, connection.getTenantId(),
                    cacheUsingTable.getViewIndexId() != null);
        } catch (SQLException e) {
            throw new IOException(e);
        }
    } else {
        tenantIdBytes = connection.getTenantId() == null ? null
                : connection.getTenantId().getBytes();
    }
    if (tenantIdBytes != null) {
        builder.setTenantId(ByteStringer.wrap(tenantIdBytes));
    }
    builder.setCacheId(ByteStringer.wrap(cacheId));
    builder.setUsePersistentCache(usePersistentCache);
    builder.setCachePtr(org.apache.phoenix.protobuf.ProtobufUtil.toProto(cachePtr));
    builder.setHasProtoBufIndexMaintainer(true);
    ServerCacheFactoryProtos.ServerCacheFactory.Builder svrCacheFactoryBuider = ServerCacheFactoryProtos.ServerCacheFactory
            .newBuilder();
    svrCacheFactoryBuider.setClassName(cacheFactory.getClass().getName());
    builder.setCacheFactory(svrCacheFactoryBuider.build());
    builder.setTxState(ByteStringer.wrap(txState));
    builder.setClientVersion(MetaDataProtocol.PHOENIX_VERSION);
    final AddServerCacheRequest request = builder.build();

    try {
        results = htable.coprocessorService(ServerCachingService.class, keyInRegion, keyInRegion,
                new Batch.Call<ServerCachingService, AddServerCacheResponse>() {
                    @Override
                    public AddServerCacheResponse call(ServerCachingService instance) throws IOException {
                        ServerRpcController controller = new ServerRpcController();
                        BlockingRpcCallback<AddServerCacheResponse> rpcCallback = new BlockingRpcCallback<AddServerCacheResponse>();
                        instance.addServerCache(controller, request, rpcCallback);
                        if (controller.getFailedOn() != null) { throw controller.getFailedOn(); }
                        return rpcCallback.get();
                    }
                });
    } catch (Throwable t) {
        throw new Exception(t);
    }
    if (results != null && results.size() == 1) { return results.values().iterator().next().getReturn(); }
    return false;
}
 
Example 15
Source File: ServerCacheClient.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Remove the cached table from all region servers
 * @throws SQLException
 * @throws IllegalStateException if hashed table cannot be removed on any region server on which it was added
 */
private void removeServerCache(final ServerCache cache, Set<HRegionLocation> remainingOnServers) throws SQLException {
    Table iterateOverTable = null;
    final byte[] cacheId = cache.getId();
    try {
        ConnectionQueryServices services = connection.getQueryServices();
        Throwable lastThrowable = null;
        final PTable cacheUsingTable = cacheUsingTableMap.get(Bytes.mapKey(cacheId));
        byte[] tableName = cacheUsingTable.getPhysicalName().getBytes();
        iterateOverTable = services.getTable(tableName);

        List<HRegionLocation> locations = services.getAllTableRegions(tableName);
        /**
         * Allow for the possibility that the region we based where to send our cache has split and been relocated
         * to another region server *after* we sent it, but before we removed it. To accommodate this, we iterate
         * through the current metadata boundaries and remove the cache once for each server that we originally sent
         * to.
         */
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(addCustomAnnotations(
                    "Removing Cache " + cacheId + " from servers.", connection));
        }
        for (HRegionLocation entry : locations) {
         // Call once per server
            if (remainingOnServers.contains(entry)) { 
                try {
                    byte[] key = getKeyInRegion(entry.getRegion().getStartKey());
                    iterateOverTable.coprocessorService(ServerCachingService.class, key, key,
                            new Batch.Call<ServerCachingService, RemoveServerCacheResponse>() {
                                @Override
                                public RemoveServerCacheResponse call(ServerCachingService instance)
                                        throws IOException {
                                    ServerRpcController controller = new ServerRpcController();
                                    BlockingRpcCallback<RemoveServerCacheResponse> rpcCallback = new BlockingRpcCallback<RemoveServerCacheResponse>();
                                    RemoveServerCacheRequest.Builder builder = RemoveServerCacheRequest
                                            .newBuilder();
                                    final byte[] tenantIdBytes;
                                    if (cacheUsingTable.isMultiTenant()) {
                                        try {
                                            tenantIdBytes = connection.getTenantId() == null ? null
                                                    : ScanUtil.getTenantIdBytes(cacheUsingTable.getRowKeySchema(),
                                                            cacheUsingTable.getBucketNum() != null,
                                                            connection.getTenantId(),
                                                            cacheUsingTable.getViewIndexId() != null);
                                        } catch (SQLException e) {
                                            throw new IOException(e);
                                        }
                                    } else {
                                        tenantIdBytes = connection.getTenantId() == null ? null
                                                : connection.getTenantId().getBytes();
                                    }
                                    if (tenantIdBytes != null) {
                                        builder.setTenantId(ByteStringer.wrap(tenantIdBytes));
                                    }
                                    builder.setCacheId(ByteStringer.wrap(cacheId));
                                    instance.removeServerCache(controller, builder.build(), rpcCallback);
                                    if (controller.getFailedOn() != null) { throw controller.getFailedOn(); }
                                    return rpcCallback.get();
                                }
                            });
                    remainingOnServers.remove(entry);
                } catch (Throwable t) {
                    lastThrowable = t;
                    LOGGER.error(addCustomAnnotations(
                            "Error trying to remove hash cache for " + entry,
                            connection), t);
                }
            }
        }
        if (!remainingOnServers.isEmpty()) {
            LOGGER.warn(addCustomAnnotations("Unable to remove hash cache for "
                            + remainingOnServers, connection),
                    lastThrowable);
        }
    } finally {
        cacheUsingTableMap.remove(Bytes.mapKey(cacheId));
        Closeables.closeQuietly(iterateOverTable);
    }
}
 
Example 16
Source File: InvalidIndexStateClientSideIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testCachedConnections() throws Throwable {
    final String schemaName = generateUniqueName();
    final String tableName = generateUniqueName();
    final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    final String indexName = generateUniqueName();
    final String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
    final Connection conn = DriverManager.getConnection(getUrl());

    // create table and indices
    String createTableSql =
            "CREATE TABLE " + fullTableName
                    + "(org_id VARCHAR NOT NULL PRIMARY KEY, v1 INTEGER, v2 INTEGER, v3 INTEGER)";
    conn.createStatement().execute(createTableSql);
    conn.createStatement()
            .execute("CREATE INDEX " + indexName + " ON " + fullTableName + "(v1)");
    conn.commit();
    PhoenixConnection phoenixConn = conn.unwrap(PhoenixConnection.class);
    ConnectionQueryServices queryServices = phoenixConn.getQueryServices();
    Table metaTable =
            phoenixConn.getQueryServices()
                    .getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
    long ts = EnvironmentEdgeManager.currentTimeMillis();
    MutationCode code =
            IndexUtil
                    .updateIndexState(fullIndexName, ts, metaTable, PIndexState.PENDING_DISABLE)
                    .getMutationCode();
    assertEquals(MutationCode.TABLE_ALREADY_EXISTS, code);
    ts = EnvironmentEdgeManager.currentTimeMillis();

    final byte[] schemaBytes = PVarchar.INSTANCE.toBytes(schemaName);
    final byte[] tableBytes = PVarchar.INSTANCE.toBytes(tableName);
    PName tenantId = phoenixConn.getTenantId();
    final long tableTimestamp = HConstants.LATEST_TIMESTAMP;
    long tableResolvedTimestamp = HConstants.LATEST_TIMESTAMP;
    final long resolvedTimestamp = tableResolvedTimestamp;
    final byte[] tenantIdBytes =
            tenantId == null ? ByteUtil.EMPTY_BYTE_ARRAY : tenantId.getBytes();
    byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes);
    Batch.Call<MetaDataService, MetaDataResponse> callable =
            new Batch.Call<MetaDataService, MetaDataResponse>() {
                @Override
                public MetaDataResponse call(MetaDataService instance) throws IOException {
                    ServerRpcController controller = new ServerRpcController();
                    BlockingRpcCallback<MetaDataResponse> rpcCallback =
                            new BlockingRpcCallback<MetaDataResponse>();
                    GetTableRequest.Builder builder = GetTableRequest.newBuilder();
                    builder.setTenantId(ByteStringer.wrap(tenantIdBytes));
                    builder.setSchemaName(ByteStringer.wrap(schemaBytes));
                    builder.setTableName(ByteStringer.wrap(tableBytes));
                    builder.setTableTimestamp(tableTimestamp);
                    builder.setClientTimestamp(resolvedTimestamp);
                    builder.setClientVersion(VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION,
                        13, PHOENIX_PATCH_NUMBER));
                    instance.getTable(controller, builder.build(), rpcCallback);
                    if (controller.getFailedOn() != null) {
                        throw controller.getFailedOn();
                    }
                    return rpcCallback.get();
                }
            };
    int version = VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION, 13, PHOENIX_PATCH_NUMBER);
    LOGGER.info("Client version: " + version);
    Table ht =
            queryServices.getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
    try {
        final Map<byte[], MetaDataResponse> results =
                ht.coprocessorService(MetaDataService.class, tableKey, tableKey, callable);

        assert (results.size() == 1);
        MetaDataResponse result = results.values().iterator().next();
        assert (result.getTable().getIndexesCount() == 1);
        assert (PIndexState.valueOf(result.getTable().getIndexes(0).getIndexState())
                .equals(PIndexState.DISABLE));
    } catch (Exception e) {
        LOGGER.error("Exception Occurred: " + e);

    } finally {
        Closeables.closeQuietly(ht);
    }

}
 
Example 17
Source File: AggregationClient.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * It helps locate the region with median for a given column whose weight
 * is specified in an optional column.
 * From individual regions, it obtains sum of values and sum of weights.
 * @param table table to scan.
 * @param ci the user's ColumnInterpreter implementation
 * @param scan the HBase scan object to use to read data from HBase
 * @return pair whose first element is a map between start row of the region
 *   and (sum of values, sum of weights) for the region, the second element is
 *   (sum of values, sum of weights) for all the regions chosen
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
private <R, S, P extends Message, Q extends Message, T extends Message>
  Pair<NavigableMap<byte[], List<S>>, List<S>>
  getMedianArgs(final Table table,
    final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
  final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
  final NavigableMap<byte[], List<S>> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
  class StdCallback implements Batch.Callback<List<S>> {
    S sumVal = null, sumWeights = null;

    public synchronized Pair<NavigableMap<byte[], List<S>>, List<S>> getMedianParams() {
      List<S> l = new ArrayList<>(2);
      l.add(sumVal);
      l.add(sumWeights);
      Pair<NavigableMap<byte[], List<S>>, List<S>> p = new Pair<>(map, l);
      return p;
    }

    @Override
    public synchronized void update(byte[] region, byte[] row, List<S> result) {
      map.put(row, result);
      sumVal = ci.add(sumVal, result.get(0));
      sumWeights = ci.add(sumWeights, result.get(1));
    }
  }
  StdCallback stdCallback = new StdCallback();
  table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(),
      new Batch.Call<AggregateService, List<S>>() {
        @Override
        public List<S> call(AggregateService instance) throws IOException {
          RpcController controller = new AggregationClientRpcController();
          CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback =
              new CoprocessorRpcUtils.BlockingRpcCallback<>();
          instance.getMedian(controller, requestArg, rpcCallback);
          AggregateResponse response = rpcCallback.get();
          if (controller.failed()) {
            throw new IOException(controller.errorText());
          }

          List<S> list = new ArrayList<>();
          for (int i = 0; i < response.getFirstPartCount(); i++) {
            ByteString b = response.getFirstPart(i);
            T t = getParsedGenericInstance(ci.getClass(), 4, b);
            S s = ci.getPromotedValueFromProto(t);
            list.add(s);
          }
          return list;
        }

      }, stdCallback);
  return stdCallback.getMedianParams();
}
 
Example 18
Source File: AggregationClient.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * It computes a global standard deviation for a given column and its value.
 * Standard deviation is square root of (average of squares -
 * average*average). From individual regions, it obtains sum, square sum and
 * number of rows. With these, the above values are computed to get the global
 * std.
 * @param table table to scan.
 * @param scan the HBase scan object to use to read data from HBase
 * @return standard deviations
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
private <R, S, P extends Message, Q extends Message, T extends Message>
  Pair<List<S>, Long> getStdArgs(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci,
        final Scan scan) throws Throwable {
  final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
  class StdCallback implements Batch.Callback<Pair<List<S>, Long>> {
    long rowCountVal = 0L;
    S sumVal = null, sumSqVal = null;

    public synchronized Pair<List<S>, Long> getStdParams() {
      List<S> l = new ArrayList<>(2);
      l.add(sumVal);
      l.add(sumSqVal);
      Pair<List<S>, Long> p = new Pair<>(l, rowCountVal);
      return p;
    }

    @Override
    public synchronized void update(byte[] region, byte[] row, Pair<List<S>, Long> result) {
      if (result.getFirst().size() > 0) {
        sumVal = ci.add(sumVal, result.getFirst().get(0));
        sumSqVal = ci.add(sumSqVal, result.getFirst().get(1));
        rowCountVal += result.getSecond();
      }
    }
  }

  StdCallback stdCallback = new StdCallback();
  table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(),
      new Batch.Call<AggregateService, Pair<List<S>, Long>>() {
        @Override
        public Pair<List<S>, Long> call(AggregateService instance) throws IOException {
          RpcController controller = new AggregationClientRpcController();
          CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback =
              new CoprocessorRpcUtils.BlockingRpcCallback<>();
          instance.getStd(controller, requestArg, rpcCallback);
          AggregateResponse response = rpcCallback.get();
          if (controller.failed()) {
            throw new IOException(controller.errorText());
          }
          Pair<List<S>, Long> pair = new Pair<>(new ArrayList<>(), 0L);
          if (response.getFirstPartCount() == 0) {
            return pair;
          }
          List<S> list = new ArrayList<>();
          for (int i = 0; i < response.getFirstPartCount(); i++) {
            ByteString b = response.getFirstPart(i);
            T t = getParsedGenericInstance(ci.getClass(), 4, b);
            S s = ci.getPromotedValueFromProto(t);
            list.add(s);
          }
          pair.setFirst(list);
          ByteBuffer bb = ByteBuffer.allocate(8).put(
              getBytesFromResponse(response.getSecondPart()));
          bb.rewind();
          pair.setSecond(bb.getLong());
          return pair;
        }
      }, stdCallback);
  return stdCallback.getStdParams();
}
 
Example 19
Source File: HBaseOperations.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public String getVersion() {
  String version = null;

  if ((options == null) || !options.isServerSideLibraryEnabled()) {
    LOGGER.warn("Serverside library not enabled, serverside version is irrelevant");
    return null;
  }
  try {
    // use Index as the type to check for version (for hbase type
    // doesn't matter anyways)
    final MetadataType type = MetadataType.INDEX;
    final String tableName = getMetadataTableName(type);
    if (!indexExists(tableName)) {
      createTable(
          new byte[0][],
          HBaseOperations.METADATA_CFS_VERSIONING,
          StringColumnFamilyFactory.getSingletonInstance(),
          getTableName(getQualifiedTableName(tableName)));
    }

    // Use the row count coprocessor
    if (options.isVerifyCoprocessors()) {
      verifyCoprocessor(
          tableName,
          "org.locationtech.geowave.datastore.hbase.coprocessors.VersionEndpoint",
          options.getCoprocessorJar());
    }
    final Table table = getTable(tableName);
    final Map<byte[], List<String>> versionInfoResponse =
        table.coprocessorService(
            VersionProtosClient.VersionService.class,
            null,
            null,
            new Batch.Call<VersionProtosClient.VersionService, List<String>>() {
              @Override
              public List<String> call(final VersionProtosClient.VersionService versionService)
                  throws IOException {
                final BlockingRpcCallback<VersionProtosClient.VersionResponse> rpcCallback =
                    new BlockingRpcCallback<>();
                versionService.version(null, VersionRequest.getDefaultInstance(), rpcCallback);
                final VersionProtosClient.VersionResponse response = rpcCallback.get();
                return response.getVersionInfoList();
              }
            });
    table.close();
    if ((versionInfoResponse == null) || versionInfoResponse.isEmpty()) {
      LOGGER.error("No response from version coprocessor");
    } else {
      final Iterator<List<String>> values = versionInfoResponse.values().iterator();

      final List<String> value = values.next();
      while (values.hasNext()) {
        final List<String> newValue = values.next();
        if (!value.equals(newValue)) {
          LOGGER.error(
              "Version Info '"
                  + Arrays.toString(value.toArray())
                  + "' and '"
                  + Arrays.toString(newValue.toArray())
                  + "' differ.  This may mean that different regions are using different versions of GeoWave.");
        }
      }
      version = VersionUtils.asLineDelimitedString(value);
    }
  } catch (final Throwable e) {
    LOGGER.warn("Unable to check metadata table for version", e);
  }
  return version;
}
 
Example 20
Source File: AggregationClient.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * It gives the minimum value of a column for a given column family for the
 * given range. In case qualifier is null, a min of all values for the given
 * family is returned.
 * @param table table to scan.
 * @param ci the user's ColumnInterpreter implementation
 * @param scan the HBase scan object to use to read data from HBase
 * @return min val &lt;R&gt;
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
public <R, S, P extends Message, Q extends Message, T extends Message>
  R min(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan)
        throws Throwable {
  final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
  class MinCallBack implements Batch.Callback<R> {
    private R min = null;

    public R getMinimum() {
      return min;
    }

    @Override
    public synchronized void update(byte[] region, byte[] row, R result) {
      min = (min == null || (result != null && ci.compare(result, min) < 0)) ? result : min;
    }
  }

  MinCallBack minCallBack = new MinCallBack();
  table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(),
      new Batch.Call<AggregateService, R>() {
        @Override
        public R call(AggregateService instance) throws IOException {
          RpcController controller = new AggregationClientRpcController();
          CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback =
              new CoprocessorRpcUtils.BlockingRpcCallback<>();
          instance.getMin(controller, requestArg, rpcCallback);
          AggregateResponse response = rpcCallback.get();
          if (controller.failed()) {
            throw new IOException(controller.errorText());
          }
          if (response.getFirstPartCount() > 0) {
            ByteString b = response.getFirstPart(0);
            Q q = getParsedGenericInstance(ci.getClass(), 3, b);
            return ci.getCellValueFromProto(q);
          }
          return null;
        }
      }, minCallBack);
  log.debug("Min fom all regions is: " + minCallBack.getMinimum());
  return minCallBack.getMinimum();
}