Java Code Examples for javax.annotation.concurrent.GuardedBy

The following examples show how to use javax.annotation.concurrent.GuardedBy. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: grpc-nebula-java   Source File: OkHttpClientStream.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("lock")
private void cancel(Status reason, boolean stopDelivery, Metadata trailers) {
  if (cancelSent) {
    return;
  }
  cancelSent = true;
  if (canStart) {
    // stream is pending.
    transport.removePendingStream(OkHttpClientStream.this);
    // release holding data, so they can be GCed or returned to pool earlier.
    requestHeaders = null;
    pendingData.clear();
    canStart = false;
    transportReportStatus(reason, true, trailers != null ? trailers : new Metadata());
  } else {
    // If pendingData is null, start must have already been called, which means synStream has
    // been called as well.
    transport.finishStream(
        id(), reason, PROCESSED, stopDelivery, ErrorCode.CANCEL, trailers);
  }
}
 
Example 2
@GuardedBy("lock")
private ReadableAttributes getImmutableAttributes() {
  if (attributes == null || attributes.isEmpty()) {
    return Attributes.empty();
  }
  // if the span has ended, then the attributes are unmodifiable,
  // so we can return them directly and save copying all the data.
  if (hasEnded) {
    return attributes;
  }
  // otherwise, make a copy of the data into an immutable container.
  Attributes.Builder builder = Attributes.newBuilder();
  for (Entry<String, AttributeValue> entry : attributes.entrySet()) {
    builder.setAttribute(entry.getKey(), entry.getValue());
  }
  return builder.build();
}
 
Example 3
Source Project: grpc-nebula-java   Source File: OkHttpClientTransport.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("lock")
private void startStream(OkHttpClientStream stream) {
  Preconditions.checkState(
      stream.id() == OkHttpClientStream.ABSENT_ID, "StreamId already assigned");
  streams.put(nextStreamId, stream);
  setInUse(stream);
  stream.transportState().start(nextStreamId);
  // For unary and server streaming, there will be a data frame soon, no need to flush the header.
  if ((stream.getType() != MethodType.UNARY && stream.getType() != MethodType.SERVER_STREAMING)
      || stream.useGet()) {
    frameWriter.flush();
  }
  if (nextStreamId >= Integer.MAX_VALUE - 2) {
    // Make sure nextStreamId greater than all used id, so that mayHaveCreatedStream() performs
    // correctly.
    nextStreamId = Integer.MAX_VALUE;
    startGoAway(Integer.MAX_VALUE, ErrorCode.NO_ERROR,
        Status.UNAVAILABLE.withDescription("Stream ids exhausted"));
  } else {
    nextStreamId += 2;
  }
}
 
Example 4
Source Project: grpc-nebula-java   Source File: OkHttpClientTransport.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("lock")
private void maybeClearInUse(OkHttpClientStream stream) {
  if (hasStream) {
    if (pendingStreams.isEmpty() && streams.isEmpty()) {
      hasStream = false;
      if (keepAliveManager != null) {
        // We don't have any active streams. No need to do keepalives any more.
        // Again, we have to call this inside the lock to avoid the race between onTransportIdle
        // and onTransportActive.
        keepAliveManager.onTransportIdle();
      }
    }
  }
  if (stream.shouldBeCountedForInUse()) {
    inUseState.updateObjectInUse(stream, false);
  }
}
 
Example 5
Source Project: presto   Source File: ClusterMemoryManager.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("this")
private boolean isLastKilledQueryGone()
{
    if (lastKilledQuery == null) {
        return true;
    }

    // If the lastKilledQuery is marked as leaked by the ClusterMemoryLeakDetector we consider the lastKilledQuery as gone,
    // so that the ClusterMemoryManager can continue to make progress even if there are leaks.
    // Even if the weak references to the leaked queries are GCed in the ClusterMemoryLeakDetector, it will mark the same queries
    // as leaked in its next run, and eventually the ClusterMemoryManager will make progress.
    if (memoryLeakDetector.wasQueryPossiblyLeaked(lastKilledQuery)) {
        lastKilledQuery = null;
        return true;
    }

    // pools fields is updated based on nodes field.
    // Therefore, if the query is gone from pools field, it should also be gone from nodes field.
    // However, since nodes can updated asynchronously, it has the potential of coming back after being gone.
    // Therefore, even if the query appears to be gone here, it might be back when one inspects nodes later.
    return !pools.get(GENERAL_POOL)
            .getQueryMemoryReservations()
            .containsKey(lastKilledQuery);
}
 
Example 6
@GuardedBy("lock")
private List<Event> getImmutableTimedEvents() {
  if (events.isEmpty()) {
    return Collections.emptyList();
  }

  List<Event> results = new ArrayList<>(events.size());
  for (TimedEvent event : events) {
    if (event instanceof RawTimedEventWithEvent) {
      // make sure to copy the data if the event is wrapping another one,
      // so we don't hold on the caller's memory
      results.add(
          TimedEvent.create(
              event.getEpochNanos(),
              event.getName(),
              event.getAttributes(),
              event.getTotalAttributeCount()));
    } else {
      results.add(event);
    }
  }
  return Collections.unmodifiableList(results);
}
 
Example 7
Source Project: presto   Source File: QueryContext.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("this")
private String getAdditionalFailureInfo(long allocated, long delta)
{
    Map<String, Long> queryAllocations = memoryPool.getTaggedMemoryAllocations().get(queryId);

    String additionalInfo = format("Allocated: %s, Delta: %s", succinctBytes(allocated), succinctBytes(delta));

    // It's possible that a query tries allocating more than the available memory
    // failing immediately before any allocation of that query is tagged
    if (queryAllocations == null) {
        return additionalInfo;
    }

    String topConsumers = queryAllocations.entrySet().stream()
            .sorted(comparingByValue(Comparator.reverseOrder()))
            .limit(3)
            .filter(e -> e.getValue() >= 0)
            .collect(toImmutableMap(Entry::getKey, e -> succinctBytes(e.getValue())))
            .toString();

    return format("%s, Top Consumers: %s", additionalInfo, topConsumers);
}
 
Example 8
Source Project: flink   Source File: EmbeddedLeaderService.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("lock")
private void shutdownInternally(Exception exceptionForHandlers) {
	assert Thread.holdsLock(lock);

	if (!shutdown) {
		// clear all leader status
		currentLeaderProposed = null;
		currentLeaderConfirmed = null;
		currentLeaderSessionId = null;
		currentLeaderAddress = null;

		// fail all registered listeners
		for (EmbeddedLeaderElectionService service : allLeaderContenders) {
			service.shutdown(exceptionForHandlers);
		}
		allLeaderContenders.clear();

		// fail all registered listeners
		for (EmbeddedLeaderRetrievalService service : listeners) {
			service.shutdown(exceptionForHandlers);
		}
		listeners.clear();

		shutdown = true;
	}
}
 
Example 9
Source Project: presto   Source File: Driver.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("exclusiveLock")
private void handleMemoryRevoke()
{
    for (int i = 0; i < activeOperators.size() && !driverContext.isDone(); i++) {
        Operator operator = activeOperators.get(i);

        if (revokingOperators.containsKey(operator)) {
            checkOperatorFinishedRevoking(operator);
        }
        else if (operator.getOperatorContext().isMemoryRevokingRequested()) {
            ListenableFuture<?> future = operator.startMemoryRevoke();
            revokingOperators.put(operator, future);
            checkOperatorFinishedRevoking(operator);
        }
    }
}
 
Example 10
Source Project: presto   Source File: SqlTaskExecution.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("this")
private void mergeIntoPendingSplits(PlanNodeId planNodeId, Set<ScheduledSplit> scheduledSplits, Set<Lifespan> noMoreSplitsForLifespan, boolean noMoreSplits)
{
    checkHoldsLock();

    DriverSplitRunnerFactory partitionedDriverFactory = driverRunnerFactoriesWithSplitLifeCycle.get(planNodeId);
    PendingSplitsForPlanNode pendingSplitsForPlanNode = pendingSplitsByPlanNode.get(planNodeId);

    partitionedDriverFactory.splitsAdded(scheduledSplits.size());
    for (ScheduledSplit scheduledSplit : scheduledSplits) {
        Lifespan lifespan = scheduledSplit.getSplit().getLifespan();
        checkLifespan(partitionedDriverFactory.getPipelineExecutionStrategy(), lifespan);
        pendingSplitsForPlanNode.getLifespan(lifespan).addSplit(scheduledSplit);
        schedulingLifespanManager.addLifespanIfAbsent(lifespan);
    }
    for (Lifespan lifespanWithNoMoreSplits : noMoreSplitsForLifespan) {
        checkLifespan(partitionedDriverFactory.getPipelineExecutionStrategy(), lifespanWithNoMoreSplits);
        pendingSplitsForPlanNode.getLifespan(lifespanWithNoMoreSplits).noMoreSplits();
        schedulingLifespanManager.addLifespanIfAbsent(lifespanWithNoMoreSplits);
    }
    if (noMoreSplits) {
        pendingSplitsForPlanNode.setNoMoreSplits();
    }
}
 
Example 11
Source Project: presto   Source File: SemiTransactionalHiveMetastore.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This method can only be called when the table is known to exist
 */
@GuardedBy("this")
private TableSource getTableSource(String databaseName, String tableName)
{
    checkHoldsLock();

    checkReadable();
    Action<TableAndMore> tableAction = tableActions.get(new SchemaTableName(databaseName, tableName));
    if (tableAction == null) {
        return TableSource.PRE_EXISTING_TABLE;
    }
    switch (tableAction.getType()) {
        case ADD:
            return TableSource.CREATED_IN_THIS_TRANSACTION;
        case DROP:
            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
        case ALTER:
        case INSERT_EXISTING:
            return TableSource.PRE_EXISTING_TABLE;
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
 
Example 12
Source Project: Flink-CEPplus   Source File: EmbeddedLeaderService.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("lock")
private void shutdownInternally(Exception exceptionForHandlers) {
	assert Thread.holdsLock(lock);

	if (!shutdown) {
		// clear all leader status
		currentLeaderProposed = null;
		currentLeaderConfirmed = null;
		currentLeaderSessionId = null;
		currentLeaderAddress = null;

		// fail all registered listeners
		for (EmbeddedLeaderElectionService service : allLeaderContenders) {
			service.shutdown(exceptionForHandlers);
		}
		allLeaderContenders.clear();

		// fail all registered listeners
		for (EmbeddedLeaderRetrievalService service : listeners) {
			service.shutdown(exceptionForHandlers);
		}
		listeners.clear();

		shutdown = true;
	}
}
 
Example 13
Source Project: Flink-CEPplus   Source File: MonotonicTTLTimeProvider.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
static long unfreezeTime() {
	synchronized (lock) {
		timeIsFrozen = false;
		return lastReturnedProcessingTime;
	}
}
 
Example 14
Source Project: grpc-nebula-java   Source File: CronetClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
@Override
public void bytesRead(int processedBytes) {
  bytesPendingProcess -= processedBytes;
  if (bytesPendingProcess == 0 && !readClosed) {
    if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
      Log.v(LOG_TAG, "BidirectionalStream.read");
    }
    stream.read(ByteBuffer.allocateDirect(READ_BUFFER_CAPACITY));
  }
}
 
Example 15
Source Project: grpc-nebula-java   Source File: CronetClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void transportHeadersReceived(Metadata metadata, boolean endOfStream) {
  if (endOfStream) {
    transportTrailersReceived(metadata);
  } else {
    transportHeadersReceived(metadata);
  }
}
 
Example 16
Source Project: flink   Source File: MiniCluster.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private Collection<? extends CompletableFuture<Void>> terminateTaskExecutors() {
	final Collection<CompletableFuture<Void>> terminationFutures = new ArrayList<>(taskManagers.size());
	for (int i = 0; i < taskManagers.size(); i++) {
		terminationFutures.add(terminateTaskExecutor(i));
	}

	return terminationFutures;
}
 
Example 17
Source Project: grpc-nebula-java   Source File: CronetClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void clearPendingData() {
  for (PendingData data : pendingData) {
    data.buffer.clear();
  }
  pendingData.clear();
}
 
Example 18
Source Project: grpc-nebula-java   Source File: CronetClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void writeAllPendingData() {
  for (PendingData data : pendingData) {
    streamWrite(data.buffer, data.endOfStream, data.flush);
  }
  pendingData.clear();
}
 
Example 19
Source Project: flink   Source File: AkkaRpcService.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
@Nonnull
private CompletableFuture<Void> terminateAkkaRpcActors() {
	final Collection<CompletableFuture<Void>> akkaRpcActorTerminationFutures = new ArrayList<>(actors.size());

	for (Map.Entry<ActorRef, RpcEndpoint> actorRefRpcEndpointEntry : actors.entrySet()) {
		akkaRpcActorTerminationFutures.add(terminateAkkaRpcActor(actorRefRpcEndpointEntry.getKey(), actorRefRpcEndpointEntry.getValue()));
	}
	actors.clear();

	return FutureUtils.waitForAll(akkaRpcActorTerminationFutures);
}
 
Example 20
Source Project: flink   Source File: BoundedBlockingSubpartition.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void checkReaderReferencesAndDispose() throws IOException {
	assert Thread.holdsLock(lock);

	// To avoid lingering memory mapped files (large resource footprint), we don't
	// wait for GC to unmap the files, but use a Netty utility to directly unmap the file.
	// To avoid segmentation faults, we need to wait until all readers have been released.

	if (readers.isEmpty()) {
		data.close();
	}
}
 
Example 21
Source Project: grpc-nebula-java   Source File: OkHttpClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@GuardedBy("lock")
public void bytesRead(int processedBytes) {
  processedWindow -= processedBytes;
  if (processedWindow <= initialWindowSize * Utils.DEFAULT_WINDOW_UPDATE_RATIO) {
    int delta = initialWindowSize - processedWindow;
    window += delta;
    processedWindow += delta;
    frameWriter.windowUpdate(id(), delta);
  }
}
 
Example 22
Source Project: grpc-nebula-java   Source File: OkHttpClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@GuardedBy("lock")
public void runOnTransportThread(final Runnable r) {
  synchronized (lock) {
    r.run();
  }
}
 
Example 23
Source Project: grpc-nebula-java   Source File: OkHttpClientStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Must be called with holding the transport lock.
 */
@GuardedBy("lock")
public void transportHeadersReceived(List<Header> headers, boolean endOfStream) {
  if (endOfStream) {
    transportTrailersReceived(Utils.convertTrailers(headers));
  } else {
    transportHeadersReceived(Utils.convertHeaders(headers));
  }
}
 
Example 24
Source Project: grpc-nebula-java   Source File: OkHttpClientStream.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void onEndOfStream() {
  if (!isOutboundClosed()) {
    // If server's end-of-stream is received before client sends end-of-stream, we just send a
    // reset to server to fully close the server side stream.
    transport.finishStream(id(),null, PROCESSED, false, ErrorCode.CANCEL, null);
  } else {
    transport.finishStream(id(), null, PROCESSED, false, null, null);
  }
}
 
Example 25
Source Project: grpc-nebula-java   Source File: OkHttpClientTransport.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
void streamReadyToStart(OkHttpClientStream clientStream) {
  if (goAwayStatus != null) {
    clientStream.transportState().transportReportStatus(
        goAwayStatus, RpcProgress.REFUSED, true, new Metadata());
  } else if (streams.size() >= maxConcurrentStreams) {
    pendingStreams.add(clientStream);
    setInUse(clientStream);
  } else {
    startStream(clientStream);
  }
}
 
Example 26
Source Project: flink   Source File: EmbeddedLeaderService.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private CompletableFuture<Void> updateLeader() {
	// this must be called under the lock
	assert Thread.holdsLock(lock);

	if (currentLeaderConfirmed == null && currentLeaderProposed == null) {
		// we need a new leader
		if (allLeaderContenders.isEmpty()) {
			// no new leader available, tell everyone that there is no leader currently
			return notifyAllListeners(null, null);
		}
		else {
			// propose a leader and ask it
			final UUID leaderSessionId = UUID.randomUUID();
			EmbeddedLeaderElectionService leaderService = allLeaderContenders.iterator().next();

			currentLeaderSessionId = leaderSessionId;
			currentLeaderProposed = leaderService;
			currentLeaderProposed.isLeader = true;

			LOG.info("Proposing leadership to contender {} @ {}",
					leaderService.contender, leaderService.contender.getAddress());

			return execute(new GrantLeadershipCall(leaderService.contender, leaderSessionId, LOG));
		}
	} else {
		return CompletableFuture.completedFuture(null);
	}
}
 
Example 27
Source Project: grpc-nebula-java   Source File: OkHttpClientTransport.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * When the transport is in goAway state, we should stop it once all active streams finish.
 */
@GuardedBy("lock")
private void stopIfNecessary() {
  if (!(goAwayStatus != null && streams.isEmpty() && pendingStreams.isEmpty())) {
    return;
  }
  if (stopped) {
    return;
  }
  stopped = true;

  if (keepAliveManager != null) {
    keepAliveManager.onTransportTermination();
    // KeepAliveManager should stop using the scheduler after onTransportTermination gets called.
    scheduler = SharedResourceHolder.release(TIMER_SERVICE, scheduler);
  }

  if (ping != null) {
    ping.failed(getPingFailure());
    ping = null;
  }

  if (!goAwaySent) {
    // Send GOAWAY with lastGoodStreamId of 0, since we don't expect any server-initiated
    // streams. The GOAWAY is part of graceful shutdown.
    goAwaySent = true;
    frameWriter.goAway(0, ErrorCode.NO_ERROR, new byte[0]);
  }

  // We will close the underlying socket in the writing thread to break out the reader
  // thread, which will close the frameReader and notify the listener.
  frameWriter.close();
}
 
Example 28
Source Project: grpc-nebula-java   Source File: AndroidChannelBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void configureNetworkMonitoring() {
  // Android N added the registerDefaultNetworkCallback API to listen to changes in the device's
  // default network. For earlier Android API levels, use the BroadcastReceiver API.
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && connectivityManager != null) {
    final DefaultNetworkCallback defaultNetworkCallback = new DefaultNetworkCallback();
    connectivityManager.registerDefaultNetworkCallback(defaultNetworkCallback);
    unregisterRunnable =
        new Runnable() {
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          @Override
          public void run() {
            connectivityManager.unregisterNetworkCallback(defaultNetworkCallback);
          }
        };
  } else {
    final NetworkReceiver networkReceiver = new NetworkReceiver();
    IntentFilter networkIntentFilter =
        new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(networkReceiver, networkIntentFilter);
    unregisterRunnable =
        new Runnable() {
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          @Override
          public void run() {
            context.unregisterReceiver(networkReceiver);
          }
        };
  }
}
 
Example 29
Source Project: flink   Source File: MiniCluster.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void startTaskManagers() throws Exception {
	final int numTaskManagers = miniClusterConfiguration.getNumTaskManagers();

	LOG.info("Starting {} TaskManger(s)", numTaskManagers);

	for (int i = 0; i < numTaskManagers; i++) {
		startTaskExecutor();
	}
}
 
Example 30
Source Project: grpc-nebula-java   Source File: InternalSubchannel.java    License: Apache License 2.0 5 votes vote down vote up
@GuardedBy("lock")
private void startNewTransport() {
  Preconditions.checkState(reconnectTask == null, "Should have no reconnectTask scheduled");

  if (addressIndex.isAtBeginning()) {
    connectingTimer.reset().start();
  }
  SocketAddress address = addressIndex.getCurrentAddress();

  ProxyParameters proxy = null;
  if (address instanceof ProxySocketAddress) {
    proxy = ((ProxySocketAddress) address).getProxyParameters();
    address = ((ProxySocketAddress) address).getAddress();
  }

  ClientTransportFactory.ClientTransportOptions options =
      new ClientTransportFactory.ClientTransportOptions()
        .setAuthority(authority)
        .setEagAttributes(addressIndex.getCurrentEagAttributes())
        .setUserAgent(userAgent)
        .setProxyParameters(proxy);
  ConnectionClientTransport transport =
      new CallTracingTransport(
          transportFactory.newClientTransport(address, options), callsTracer);
  channelz.addClientSocket(transport);
  pendingTransport = transport;
  transports.add(transport);
  Runnable runnable = transport.start(new TransportListener(transport, address));
  if (runnable != null) {
    syncContext.executeLater(runnable);
  }

  logger.info("创建客户端与服务端[" + address + "]连接");
}