Search in sources :

Example 1 with Condition

use of java.util.concurrent.locks.Condition in project hive by apache.

the class HiveMetaStore method main.

/**
   * @param args
   */
public static void main(String[] args) throws Throwable {
    HiveConf.setLoadMetastoreConfig(true);
    final HiveConf conf = new HiveConf(HMSHandler.class);
    HiveMetastoreCli cli = new HiveMetastoreCli(conf);
    cli.parse(args);
    final boolean isCliVerbose = cli.isVerbose();
    // NOTE: It is critical to do this prior to initializing log4j, otherwise
    // any log specific settings via hiveconf will be ignored
    Properties hiveconf = cli.addHiveconfToSystemProperties();
    // use Hive's default log4j configuration
    if (System.getProperty("log4j.configurationFile") == null) {
        // before any of the other core hive classes are loaded
        try {
            LogUtils.initHiveLog4j();
        } catch (LogInitializationException e) {
            HMSHandler.LOG.warn(e.getMessage());
        }
    }
    HiveStringUtils.startupShutdownMessage(HiveMetaStore.class, args, LOG);
    try {
        String msg = "Starting hive metastore on port " + cli.port;
        HMSHandler.LOG.info(msg);
        if (cli.isVerbose()) {
            System.err.println(msg);
        }
        // set all properties specified on the command line
        for (Map.Entry<Object, Object> item : hiveconf.entrySet()) {
            conf.set((String) item.getKey(), (String) item.getValue());
        }
        // Add shutdown hook.
        ShutdownHookManager.addShutdownHook(new Runnable() {

            @Override
            public void run() {
                String shutdownMsg = "Shutting down hive metastore.";
                HMSHandler.LOG.info(shutdownMsg);
                if (isCliVerbose) {
                    System.err.println(shutdownMsg);
                }
                if (conf.getBoolVar(ConfVars.METASTORE_METRICS)) {
                    try {
                        MetricsFactory.close();
                    } catch (Exception e) {
                        LOG.error("error in Metrics deinit: " + e.getClass().getName() + " " + e.getMessage(), e);
                    }
                }
            }
        });
        //Start Metrics for Standalone (Remote) Mode
        if (conf.getBoolVar(ConfVars.METASTORE_METRICS)) {
            try {
                MetricsFactory.init(conf);
            } catch (Exception e) {
                // log exception, but ignore inability to start
                LOG.error("error in Metrics init: " + e.getClass().getName() + " " + e.getMessage(), e);
            }
        }
        Lock startLock = new ReentrantLock();
        Condition startCondition = startLock.newCondition();
        AtomicBoolean startedServing = new AtomicBoolean();
        startMetaStoreThreads(conf, startLock, startCondition, startedServing);
        startMetaStore(cli.getPort(), ShimLoader.getHadoopThriftAuthBridge(), conf, startLock, startCondition, startedServing);
    } catch (Throwable t) {
        // Catch the exception, log it and rethrow it.
        HMSHandler.LOG.error("Metastore Thrift Server threw an exception...", t);
        throw t;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) TException(org.apache.thrift.TException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 2 with Condition

use of java.util.concurrent.locks.Condition in project ribbon by Netflix.

the class TestUtils method waitUntilTrueOrTimeout.

public static void waitUntilTrueOrTimeout(int timeoutMilliseconds, final Func0<Boolean> func) {
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    final AtomicBoolean stopThread = new AtomicBoolean(false);
    if (!func.call()) {
        (new Thread() {

            @Override
            public void run() {
                while (!stopThread.get()) {
                    if (func.call()) {
                        lock.lock();
                        try {
                            condition.signalAll();
                        } finally {
                            lock.unlock();
                        }
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        lock.lock();
        try {
            condition.await(timeoutMilliseconds, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            stopThread.set(true);
        }
    }
    assertTrue(func.call());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 3 with Condition

use of java.util.concurrent.locks.Condition in project quasar by puniverse.

the class GlxGlobalRegistry method getActor0.

private CacheEntry getActor0(final String rootName, long timeout, TimeUnit unit) throws SuspendExecution, RuntimeException, InterruptedException {
    final long deadline = unit != null ? System.nanoTime() + unit.toNanos(timeout) : 0;
    final Store store = grid.store();
    final long root;
    final ReentrantLock lck0 = new ReentrantLock();
    final Condition cond = lck0.newCondition();
    boolean listening = false;
    final StoreTransaction txn = store.beginTransaction();
    try {
        root = store.getRoot(rootName, txn);
        final CacheListener listener = new AbstractCacheListener() {

            @Override
            public void evicted(Cache cache, long id) {
                invalidated(cache, id);
            }

            @Override
            public void invalidated(Cache cache, long id) {
                grid.getDelegate().store().getAsync(id);
            }

            @Override
            public void received(Cache cache, long id, long version, ByteBuffer data) {
                if (data != null && data.remaining() > 0) {
                    LOG.debug("Received root {} ({})", rootName, Long.toHexString(id));
                    lck0.lock();
                    try {
                        cond.signalAll();
                    } finally {
                        lck0.unlock();
                    }
                    store.setListener(root, null);
                }
            }
        };
        listening = (store.setListenerIfAbsent(root, listener) == listener);
        store.commit(txn);
    } catch (TimeoutException e) {
        LOG.error("Getting actor {} failed due to timeout", rootName);
        store.rollback(txn);
        store.abort(txn);
        throw new RuntimeException("Actor discovery failed");
    }
    try {
        byte[] buf = store.gets(root, null);
        long version = store.getVersion(root);
        store.release(root);
        if (listening) {
            lck0.lock();
            try {
                while (buf == null || buf.length == 0) {
                    LOG.debug("Store returned null for root {}", rootName);
                    if (deadline > 0) {
                        final long now = System.nanoTime();
                        if (now > deadline)
                            // throw new java.util.concurrent.TimeoutException();
                            return null;
                        cond.await(deadline - now, TimeUnit.NANOSECONDS);
                    } else
                        cond.await();
                    buf = store.gets(root, null);
                    version = store.getVersion(root);
                    store.release(root);
                }
            } finally {
                lck0.unlock();
            }
        } else
            assert buf != null && buf.length > 0;
        final ActorRef<?> actor = deserActor(rootName, buf);
        return new CacheEntry(actor, root, version);
    } catch (TimeoutException e) {
        LOG.error("Getting actor {} failed due to timeout", rootName);
        throw new RuntimeException("Actor discovery failed");
    }
}
Also used : ReentrantLock(co.paralleluniverse.strands.concurrent.ReentrantLock) Condition(java.util.concurrent.locks.Condition) StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) Store(co.paralleluniverse.galaxy.quasar.Store) ByteBuffer(java.nio.ByteBuffer) CacheListener(co.paralleluniverse.galaxy.CacheListener) AbstractCacheListener(co.paralleluniverse.galaxy.AbstractCacheListener) AbstractCacheListener(co.paralleluniverse.galaxy.AbstractCacheListener) Cache(co.paralleluniverse.galaxy.Cache) TimeoutException(co.paralleluniverse.galaxy.TimeoutException)

Example 4 with Condition

use of java.util.concurrent.locks.Condition in project RoboZombie by sahan.

the class AsyncEndpointTest method successScenario.

/**
	 * <p>See {@link #testAsyncSuccess()}.</p>
	 */
private void successScenario() throws InterruptedException {
    String subpath = "/asyncsuccess", body = "hello";
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200).withBody(body)));
    final Object[] content = new Object[2];
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    String result = asyncEndpoint.asyncSuccess(new AsyncHandler<String>() {

        @Override
        public void onSuccess(HttpResponse httpResponse, String deserializedContent) {
            lock.lock();
            content[0] = httpResponse;
            content[1] = deserializedContent;
            condition.signal();
            lock.unlock();
        }
    });
    lock.lock();
    condition.await();
    lock.unlock();
    verify(getRequestedFor(urlEqualTo(subpath)));
    assertTrue(content[0] != null);
    assertTrue(content[1] != null);
    assertTrue(content[1].equals(body));
    assertNull(result);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) HttpResponse(org.apache.http.HttpResponse) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 5 with Condition

use of java.util.concurrent.locks.Condition in project jstorm by alibaba.

the class NettyClientAsync method waitforFlowCtrlAndSend.

private void waitforFlowCtrlAndSend(TaskMessage message) {
    int targetTaskId = message.task();
    boolean isSend = true;
    Condition condition = null;
    MessageBatch flushCache = null;
    try {
        flowCtrlLock.lock();
        condition = targetTasksUnderFlowCtrl.get(targetTaskId);
        // If target task is under flow control
        if (condition != null) {
            MessageBatch cache = targetTasksCache.get(targetTaskId);
            if (cache == null) {
                cache = new MessageBatch(cacheSize);
                targetTasksCache.put(targetTaskId, cache);
            }
            cache.add(message);
            if (cache.isFull()) {
                flushCache = targetTasksCache.remove(targetTaskId);
            } else {
                isSend = false;
            }
        }
    } finally {
        flowCtrlLock.unlock();
    }
    if (isSend) {
        // Cache is full. Try to flush till flow control is released.
        if (flushCache != null) {
            //LOG.debug("Flow Ctrl: Wait for target task-{}", targetTaskId);
            long pendingTime = 0;
            boolean done = false;
            while (condition != null && !done) {
                try {
                    lock.lock();
                    done = condition.await(flowCtrlAwaitTime, TimeUnit.MILLISECONDS);
                    pendingTime += flowCtrlAwaitTime;
                    if (timeoutMs != -1 && pendingTime >= timeoutMs) {
                        LOG.warn("Discard message under flow ctrl due to pending message timeout({}ms), messageSize={}", timeoutMs, flushCache.getEncodedLength());
                        targetTasksUnderFlowCtrl.remove(targetTaskId);
                        return;
                    }
                    if (pendingTime % 30000 == 0) {
                        LOG.info("Pending total time={} since target task-{} is under flow control ", pendingTime, targetTaskId);
                    }
                } catch (InterruptedException e) {
                    LOG.info("flow control was interrupted! targetTask-{}", targetTaskId);
                } finally {
                    lock.unlock();
                }
                try {
                    flowCtrlLock.lock();
                    condition = targetTasksUnderFlowCtrl.get(targetTaskId);
                } finally {
                    flowCtrlLock.unlock();
                }
            }
            pushBatch(flushCache);
        } else {
            pushBatch(message);
        }
    }
}
Also used : Condition(java.util.concurrent.locks.Condition)

Aggregations

Condition (java.util.concurrent.locks.Condition)34 ReentrantLock (java.util.concurrent.locks.ReentrantLock)20 Lock (java.util.concurrent.locks.Lock)14 Test (org.junit.Test)9 HttpResponse (org.apache.http.HttpResponse)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ByteBuffer (java.nio.ByteBuffer)3 User (com.lonepulse.robozombie.model.User)2 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)2 UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 Event (org.apache.ignite.events.Event)2 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)2 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)2 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)2 Test (org.testng.annotations.Test)2 TaskMessage (backtype.storm.messaging.TaskMessage)1 AbstractCacheListener (co.paralleluniverse.galaxy.AbstractCacheListener)1