Search in sources :

Example 51 with Reference

use of java.lang.ref.Reference in project geode by apache.

the class ConnectionTable method getThreadOwnedConnection.

/**
   * Must be looking for an ordered connection that this thread owns
   * 
   * @param id stub on which to create the connection
   * @param startTime the ms clock start time for the operation
   * @param ackTimeout the ms ack-wait-threshold, or zero
   * @param ackSATimeout the ms ack-severe-alert-threshold, or zero
   * @return the connection, or null if an error
   * @throws IOException if the connection could not be created
   * @throws DistributedSystemDisconnectedException
   */
Connection getThreadOwnedConnection(DistributedMember id, long startTime, long ackTimeout, long ackSATimeout) throws IOException, DistributedSystemDisconnectedException {
    Connection result = null;
    // Look for result in the thread local
    Map m = (Map) this.threadOrderedConnMap.get();
    if (m == null) {
        // First time for this thread. Create thread local
        m = new HashMap();
        synchronized (this.threadConnMaps) {
            if (this.closed) {
                owner.getCancelCriterion().checkCancelInProgress(null);
                throw new DistributedSystemDisconnectedException(LocalizedStrings.ConnectionTable_CONNECTION_TABLE_IS_CLOSED.toLocalizedString());
            }
            // check for stale references and remove them.
            for (Iterator it = this.threadConnMaps.iterator(); it.hasNext(); ) {
                Reference r = (Reference) it.next();
                if (r.get() == null) {
                    it.remove();
                }
            }
            // for
            // ref added for bug 38011
            this.threadConnMaps.add(new WeakReference(m));
        }
        // synchronized
        this.threadOrderedConnMap.set(m);
    } else {
        // Consult thread local.
        synchronized (m) {
            result = (Connection) m.get(id);
        }
        if (result != null && result.timedOut) {
            result = null;
        }
    }
    if (result != null)
        return result;
    // OK, we have to create a new connection.
    result = Connection.createSender(owner.getMembershipManager(), this, true, /* preserveOrder */
    id, false, /* shared */
    startTime, ackTimeout, ackSATimeout);
    if (logger.isDebugEnabled()) {
        logger.debug("ConnectionTable: created an ordered connection: {}", result);
    }
    this.owner.stats.incSenders(false, /* shared */
    true);
    if (this.threadConnectionMap == null) {
        // This instance is being destroyed; fail the operation
        closeCon(LocalizedStrings.ConnectionTable_CONNECTION_TABLE_BEING_DESTROYED.toLocalizedString(), result);
        return null;
    }
    ArrayList al = (ArrayList) this.threadConnectionMap.get(id);
    if (al == null) {
        // First connection for this DistributedMember. Make sure list for this
        // stub is created if it isn't already there.
        al = new ArrayList();
        // Since it's a concurrent map, we just try to put it and then
        // return whichever we got.
        Object o = this.threadConnectionMap.putIfAbsent(id, al);
        if (o != null) {
            al = (ArrayList) o;
        }
    }
    // Add our Connection to the list
    synchronized (al) {
        al.add(result);
    }
    // Finally, add the connection to our thread local map.
    synchronized (m) {
        m.put(id, result);
    }
    scheduleIdleTimeout(result);
    return result;
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) WeakReference(java.lang.ref.WeakReference) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 52 with Reference

use of java.lang.ref.Reference in project geode by apache.

the class ConnectionTable method close.

protected void close() {
    /*
     * NOMUX if (inputMuxManager != null) { inputMuxManager.stop(); }
     */
    if (this.closed) {
        return;
    }
    this.closed = true;
    synchronized (this) {
        if (this.idleConnTimer != null) {
            this.idleConnTimer.cancel();
        }
    }
    synchronized (this.orderedConnectionMap) {
        for (Iterator it = this.orderedConnectionMap.values().iterator(); it.hasNext(); ) {
            closeCon(LocalizedStrings.ConnectionTable_CONNECTION_TABLE_BEING_DESTROYED.toLocalizedString(), it.next());
        }
        this.orderedConnectionMap.clear();
    }
    synchronized (this.unorderedConnectionMap) {
        for (Iterator it = this.unorderedConnectionMap.values().iterator(); it.hasNext(); ) {
            closeCon(LocalizedStrings.ConnectionTable_CONNECTION_TABLE_BEING_DESTROYED.toLocalizedString(), it.next());
        }
        this.unorderedConnectionMap.clear();
    }
    if (this.threadConnectionMap != null) {
        this.threadConnectionMap = null;
    }
    if (this.threadConnMaps != null) {
        synchronized (this.threadConnMaps) {
            for (Iterator it = this.threadConnMaps.iterator(); it.hasNext(); ) {
                Reference r = (Reference) it.next();
                Map m = (Map) r.get();
                if (m != null) {
                    synchronized (m) {
                        for (Iterator mit = m.values().iterator(); mit.hasNext(); ) {
                            closeCon(LocalizedStrings.ConnectionTable_CONNECTION_TABLE_BEING_DESTROYED.toLocalizedString(), mit.next());
                        }
                    }
                }
            }
            this.threadConnMaps.clear();
        }
    }
    {
        Executor localExec = this.p2pReaderThreadPool;
        if (localExec != null) {
            if (localExec instanceof ExecutorService) {
                ((ExecutorService) localExec).shutdown();
            }
        }
    }
    closeReceivers(false);
    Map m = (Map) this.threadOrderedConnMap.get();
    if (m != null) {
        synchronized (m) {
            m.clear();
        }
    }
    this.socketCloser.close();
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) AtomicReference(java.util.concurrent.atomic.AtomicReference) WeakReference(java.lang.ref.WeakReference) Reference(java.lang.ref.Reference) Iterator(java.util.Iterator) ExecutorService(java.util.concurrent.ExecutorService) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 53 with Reference

use of java.lang.ref.Reference in project jdk8u_jdk by JetBrains.

the class LFGarbageCollectedTest method collectLambdaForm.

private void collectLambdaForm() throws IllegalAccessException {
    // Usually, 2 System.GCs are necessary to enqueue a SoftReference.
    System.gc();
    System.gc();
    Reference ref = null;
    for (int i = 0; i < 10; i++) {
        try {
            ref = rq.remove(1000);
        } catch (InterruptedException e) {
        /* ignore */
        }
        if (ref != null) {
            break;
        }
        // If the reference hasn't been queued yet, trigger one more GC.
        System.gc();
    }
    if (ref == null) {
        dumpTestData();
        System.err.println("Method type: " + mtype);
        System.err.println("LambdaForm:  " + REF_FIELD.get(ph));
        if (HEAP_DUMP) {
            // Trigger OOM to force heap dump for post-mortem analysis.
            val = new long[1_000_000_000];
        }
        throw new AssertionError("Error: LambdaForm is not garbage collected");
    }
    ;
}
Also used : PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference)

Example 54 with Reference

use of java.lang.ref.Reference in project jdk8u_jdk by JetBrains.

the class ConnectionAcceptor method checkConnectPermission.

/**
     * Checks if the current caller has sufficient privilege to make
     * a connection to the remote endpoint.
     * @exception SecurityException if caller is not allowed to use this
     * Channel.
     */
private void checkConnectPermission() throws SecurityException {
    SecurityManager security = System.getSecurityManager();
    if (security == null)
        return;
    if (security != cacheSecurityManager) {
        // The security manager changed: flush the cache
        okContext = null;
        authcache = new WeakHashMap<AccessControlContext, Reference<AccessControlContext>>();
        cacheSecurityManager = security;
    }
    AccessControlContext ctx = AccessController.getContext();
    // appears in the cache, bypass the checkConnect.
    if (okContext == null || !(okContext.equals(ctx) || authcache.containsKey(ctx))) {
        security.checkConnect(ep.getHost(), ep.getPort());
        authcache.put(ctx, new SoftReference<AccessControlContext>(ctx));
    // A WeakHashMap is transformed into a SoftHashSet by making
    // each value softly refer to its own key (Peter's idea).
    }
    okContext = ctx;
}
Also used : AccessControlContext(java.security.AccessControlContext) SoftReference(java.lang.ref.SoftReference) Reference(java.lang.ref.Reference)

Example 55 with Reference

use of java.lang.ref.Reference in project cdap by caskdata.

the class BaseHiveExploreService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting {}...", BaseHiveExploreService.class.getSimpleName());
    HiveConf hiveConf = getHiveConf();
    setupSparkConf();
    cliService.init(hiveConf);
    cliService.start();
    metastoreClientsExecutorService.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            Reference<? extends Supplier<IMetaStoreClient>> ref = metastoreClientReferenceQueue.poll();
            while (ref != null) {
                IMetaStoreClient client = metastoreClientReferences.remove(ref);
                if (client != null) {
                    closeMetastoreClient(client);
                }
                ref = metastoreClientReferenceQueue.poll();
            }
        }
    }, METASTORE_CLIENT_CLEANUP_PERIOD, METASTORE_CLIENT_CLEANUP_PERIOD, TimeUnit.SECONDS);
    // Schedule the cache cleanup
    scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            runCacheCleanup();
        }
    }, cleanupJobSchedule, cleanupJobSchedule, TimeUnit.SECONDS);
}
Also used : Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Supplier(com.google.common.base.Supplier) IMetaStoreClient(org.apache.hadoop.hive.metastore.IMetaStoreClient)

Aggregations

Reference (java.lang.ref.Reference)74 WeakReference (java.lang.ref.WeakReference)34 SoftReference (java.lang.ref.SoftReference)33 ReferenceQueue (java.lang.ref.ReferenceQueue)13 Field (java.lang.reflect.Field)13 Map (java.util.Map)13 HashMap (java.util.HashMap)11 PhantomReference (java.lang.ref.PhantomReference)10 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Tracing (brave.Tracing)3 Platform (brave.internal.Platform)3 TraceContext (brave.propagation.TraceContext)3 BufferedImage (java.awt.image.BufferedImage)3 VolatileImage (java.awt.image.VolatileImage)3 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 Iterator (java.util.Iterator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3