Search in sources :

Example 1 with Scope

use of io.opentelemetry.context.Scope in project hbase by apache.

the class AbstractRpcClient method callMethod.

private Call callMethod(final Descriptors.MethodDescriptor md, final HBaseRpcController hrc, final Message param, Message returnType, final User ticket, final Address addr, final RpcCallback<Message> callback) {
    Span span = new IpcClientSpanBuilder().setMethodDescriptor(md).setRemoteAddress(addr).build();
    try (Scope scope = span.makeCurrent()) {
        final MetricsConnection.CallStats cs = MetricsConnection.newCallStats();
        cs.setStartTime(EnvironmentEdgeManager.currentTime());
        if (param instanceof ClientProtos.MultiRequest) {
            ClientProtos.MultiRequest req = (ClientProtos.MultiRequest) param;
            int numActions = 0;
            for (ClientProtos.RegionAction regionAction : req.getRegionActionList()) {
                numActions += regionAction.getActionCount();
            }
            cs.setNumActionsPerServer(numActions);
        }
        final AtomicInteger counter = concurrentCounterCache.getUnchecked(addr);
        Call call = new Call(nextCallId(), md, param, hrc.cellScanner(), returnType, hrc.getCallTimeout(), hrc.getPriority(), new RpcCallback<Call>() {

            @Override
            public void run(Call call) {
                try (Scope scope = call.span.makeCurrent()) {
                    counter.decrementAndGet();
                    onCallFinished(call, hrc, addr, callback);
                } finally {
                    if (hrc.failed()) {
                        TraceUtil.setError(span, hrc.getFailed());
                    } else {
                        span.setStatus(StatusCode.OK);
                    }
                    span.end();
                }
            }
        }, cs);
        ConnectionId remoteId = new ConnectionId(ticket, md.getService().getName(), addr);
        int count = counter.incrementAndGet();
        try {
            if (count > maxConcurrentCallsPerServer) {
                throw new ServerTooBusyException(addr, count);
            }
            cs.setConcurrentCallsPerServer(count);
            T connection = getConnection(remoteId);
            connection.sendRequest(call, hrc);
        } catch (Exception e) {
            call.setException(toIOE(e));
            span.end();
        }
        return call;
    }
}
Also used : Span(io.opentelemetry.api.trace.Span) IPCUtil.wrapException(org.apache.hadoop.hbase.ipc.IPCUtil.wrapException) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) IOException(java.io.IOException) RemoteException(org.apache.hadoop.ipc.RemoteException) IpcClientSpanBuilder(org.apache.hadoop.hbase.client.trace.IpcClientSpanBuilder) Scope(io.opentelemetry.context.Scope) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetricsConnection(org.apache.hadoop.hbase.client.MetricsConnection) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)

Example 2 with Scope

use of io.opentelemetry.context.Scope in project hbase by apache.

the class TraceUtil method trace.

public static <T> T trace(IOExceptionCallable<T> callable, Supplier<Span> creator) throws IOException {
    Span span = creator.get();
    try (Scope scope = span.makeCurrent()) {
        T ret = callable.call();
        span.setStatus(StatusCode.OK);
        return ret;
    } catch (Throwable e) {
        setError(span, e);
        throw e;
    } finally {
        span.end();
    }
}
Also used : Scope(io.opentelemetry.context.Scope) Span(io.opentelemetry.api.trace.Span)

Example 3 with Scope

use of io.opentelemetry.context.Scope in project hbase by apache.

the class TraceUtil method tracedFutures.

/**
 * Trace an asynchronous operation, and finish the create {@link Span} when all the given
 * {@code futures} are completed.
 */
public static <T> List<CompletableFuture<T>> tracedFutures(Supplier<List<CompletableFuture<T>>> action, Supplier<Span> spanSupplier) {
    Span span = spanSupplier.get();
    try (Scope scope = span.makeCurrent()) {
        List<CompletableFuture<T>> futures = action.get();
        endSpan(CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])), span);
        return futures;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Scope(io.opentelemetry.context.Scope) Span(io.opentelemetry.api.trace.Span)

Example 4 with Scope

use of io.opentelemetry.context.Scope in project hbase by apache.

the class TraceUtil method trace.

public static void trace(Runnable action, Supplier<Span> creator) {
    Span span = creator.get();
    try (Scope scope = span.makeCurrent()) {
        action.run();
        span.setStatus(StatusCode.OK);
    } catch (Throwable e) {
        setError(span, e);
        throw e;
    } finally {
        span.end();
    }
}
Also used : Scope(io.opentelemetry.context.Scope) Span(io.opentelemetry.api.trace.Span)

Example 5 with Scope

use of io.opentelemetry.context.Scope in project hbase by apache.

the class HFileReaderImpl method readBlock.

@Override
public HFileBlock readBlock(long dataBlockOffset, long onDiskBlockSize, final boolean cacheBlock, boolean pread, final boolean isCompaction, boolean updateCacheMetrics, BlockType expectedBlockType, DataBlockEncoding expectedDataBlockEncoding) throws IOException {
    if (dataBlockIndexReader == null) {
        throw new IOException(path + " block index not loaded");
    }
    long trailerOffset = trailer.getLoadOnOpenDataOffset();
    if (dataBlockOffset < 0 || dataBlockOffset >= trailerOffset) {
        throw new IOException("Requested block is out of range: " + dataBlockOffset + ", lastDataBlockOffset: " + trailer.getLastDataBlockOffset() + ", trailer.getLoadOnOpenDataOffset: " + trailerOffset + ", path=" + path);
    }
    // For any given block from any given file, synchronize reads for said
    // block.
    // Without a cache, this synchronizing is needless overhead, but really
    // the other choice is to duplicate work (which the cache would prevent you
    // from doing).
    BlockCacheKey cacheKey = new BlockCacheKey(name, dataBlockOffset, this.isPrimaryReplicaReader(), expectedBlockType);
    boolean useLock = false;
    IdLock.Entry lockEntry = null;
    Span span = TraceUtil.getGlobalTracer().spanBuilder("HFileReaderImpl.readBlock").startSpan();
    try (Scope traceScope = span.makeCurrent()) {
        while (true) {
            // Check cache for block. If found return.
            if (cacheConf.shouldReadBlockFromCache(expectedBlockType)) {
                if (useLock) {
                    lockEntry = offsetLock.getLockEntry(dataBlockOffset);
                }
                // Try and get the block from the block cache. If the useLock variable is true then this
                // is the second time through the loop and it should not be counted as a block cache miss.
                HFileBlock cachedBlock = getCachedBlock(cacheKey, cacheBlock, useLock, isCompaction, updateCacheMetrics, expectedBlockType, expectedDataBlockEncoding);
                if (cachedBlock != null) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("From Cache " + cachedBlock);
                    }
                    span.addEvent("blockCacheHit");
                    assert cachedBlock.isUnpacked() : "Packed block leak.";
                    if (cachedBlock.getBlockType().isData()) {
                        if (updateCacheMetrics) {
                            HFile.DATABLOCK_READ_COUNT.increment();
                        }
                        // type in the cache key, and we expect it to match on a cache hit.
                        if (cachedBlock.getDataBlockEncoding() != dataBlockEncoder.getDataBlockEncoding()) {
                            // Remember to release the block when in exceptional path.
                            cacheConf.getBlockCache().ifPresent(cache -> {
                                returnAndEvictBlock(cache, cacheKey, cachedBlock);
                            });
                            throw new IOException("Cached block under key " + cacheKey + " " + "has wrong encoding: " + cachedBlock.getDataBlockEncoding() + " (expected: " + dataBlockEncoder.getDataBlockEncoding() + "), path=" + path);
                        }
                    }
                    // Cache-hit. Return!
                    return cachedBlock;
                }
                if (!useLock && cacheBlock && cacheConf.shouldLockOnCacheMiss(expectedBlockType)) {
                    // check cache again with lock
                    useLock = true;
                    continue;
                }
            // Carry on, please load.
            }
            span.addEvent("blockCacheMiss");
            // Load block from filesystem.
            HFileBlock hfileBlock = fsBlockReader.readBlockData(dataBlockOffset, onDiskBlockSize, pread, !isCompaction, shouldUseHeap(expectedBlockType));
            validateBlockType(hfileBlock, expectedBlockType);
            HFileBlock unpacked = hfileBlock.unpack(hfileContext, fsBlockReader);
            BlockType.BlockCategory category = hfileBlock.getBlockType().getCategory();
            // Cache the block if necessary
            cacheConf.getBlockCache().ifPresent(cache -> {
                if (cacheBlock && cacheConf.shouldCacheBlockOnRead(category)) {
                    cache.cacheBlock(cacheKey, cacheConf.shouldCacheCompressed(category) ? hfileBlock : unpacked, cacheConf.isInMemory());
                }
            });
            if (unpacked != hfileBlock) {
                // End of life here if hfileBlock is an independent block.
                hfileBlock.release();
            }
            if (updateCacheMetrics && hfileBlock.getBlockType().isData()) {
                HFile.DATABLOCK_READ_COUNT.increment();
            }
            return unpacked;
        }
    } finally {
        if (lockEntry != null) {
            offsetLock.releaseLockEntry(lockEntry);
        }
        span.end();
    }
}
Also used : IdLock(org.apache.hadoop.hbase.util.IdLock) Scope(io.opentelemetry.context.Scope) IOException(java.io.IOException) Span(io.opentelemetry.api.trace.Span)

Aggregations

Span (io.opentelemetry.api.trace.Span)21 Scope (io.opentelemetry.context.Scope)21 IOException (java.io.IOException)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 RetryCounter (org.apache.hadoop.hbase.util.RetryCounter)3 KeeperException (org.apache.zookeeper.KeeperException)3 Context (io.opentelemetry.context.Context)2 InetSocketAddress (java.net.InetSocketAddress)2 CellScanner (org.apache.hadoop.hbase.CellScanner)2 Result (org.apache.hadoop.hbase.client.Result)2 Table (org.apache.hadoop.hbase.client.Table)2 Message (org.apache.hbase.thirdparty.com.google.protobuf.Message)2 ConsoleReporter (com.codahale.metrics.ConsoleReporter)1 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)1 StatusCode (io.opentelemetry.api.trace.StatusCode)1 Tracer (io.opentelemetry.api.trace.Tracer)1 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)1 TextMapPropagator (io.opentelemetry.context.propagation.TextMapPropagator)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Arrays (java.util.Arrays)1