Search in sources :

Example 1 with Observer

use of com.ctrip.xpipe.api.observer.Observer in project x-pipe by ctripcorp.

the class AbstractRedisKeeperTest method createReplicationStoreManager.

protected ReplicationStoreManager createReplicationStoreManager(String clusterId, String shardId, String keeperRunid, KeeperConfig keeperConfig, File storeDir) {
    DefaultReplicationStoreManager replicationStoreManager = new DefaultReplicationStoreManager(keeperConfig, clusterId, shardId, keeperRunid, storeDir, createkeeperMonitor());
    replicationStoreManager.addObserver(new Observer() {

        @Override
        public void update(Object args, Observable observable) {
            if (args instanceof NodeAdded) {
                @SuppressWarnings("unchecked") ReplicationStore replicationStore = ((NodeAdded<ReplicationStore>) args).getNode();
                try {
                    replicationStore.getMetaStore().becomeActive();
                } catch (IOException e) {
                    logger.error("[update]" + replicationStore, e);
                }
            }
        }
    });
    return replicationStoreManager;
}
Also used : Observer(com.ctrip.xpipe.api.observer.Observer) DefaultReplicationStoreManager(com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStoreManager) NodeAdded(com.ctrip.xpipe.observer.NodeAdded) IOException(java.io.IOException) DefaultReplicationStore(com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStore) Observable(com.ctrip.xpipe.api.observer.Observable)

Example 2 with Observer

use of com.ctrip.xpipe.api.observer.Observer in project x-pipe by ctripcorp.

the class HealthStatusTest method testDown.

@Test
public void testDown() {
    AtomicBoolean isDown = new AtomicBoolean();
    HealthStatus healthStatus = new HealthStatus(hostPort, () -> downAfterMilli, () -> healthDelayMilli, scheduled);
    healthStatus.addObserver(new Observer() {

        @Override
        public void update(Object args, Observable observable) {
            logger.debug("{}, {}", args, observable);
            if (args instanceof InstanceUp) {
                isDown.set(false);
            } else if (args instanceof InstanceDown) {
                isDown.set(true);
            } else {
                throw new IllegalStateException("unknown " + args);
            }
        }
    });
    healthStatus.delay(healthDelayMilli);
    Assert.assertFalse(isDown.get());
    sleep(downAfterMilli * 3);
    Assert.assertTrue(isDown.get());
    healthStatus.delay(healthDelayMilli);
    Assert.assertFalse(isDown.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Observer(com.ctrip.xpipe.api.observer.Observer) Observable(com.ctrip.xpipe.api.observer.Observable) Test(org.junit.Test) AbstractConsoleTest(com.ctrip.xpipe.redis.console.AbstractConsoleTest)

Example 3 with Observer

use of com.ctrip.xpipe.api.observer.Observer in project x-pipe by ctripcorp.

the class DefaultRedisKeeperServer method clientConnected.

@Override
public RedisClient clientConnected(Channel channel) {
    RedisClient redisClient = new DefaultRedisClient(channel, this);
    redisClients.put(channel, redisClient);
    redisClient.addObserver(new Observer() {

        @Override
        public void update(Object args, Observable observable) {
            if (args instanceof RedisSlave) {
                becomeSlave(((RedisClient) observable).channel(), (RedisSlave) args);
            }
        }
    });
    return redisClient;
}
Also used : Observer(com.ctrip.xpipe.api.observer.Observer) Observable(com.ctrip.xpipe.api.observer.Observable)

Example 4 with Observer

use of com.ctrip.xpipe.api.observer.Observer in project x-pipe by ctripcorp.

the class LifecycleObservableAbstractTest method testConcurrent.

@Test
public void testConcurrent() {
    int count = 100;
    AtomicBoolean nullObserver = new AtomicBoolean(false);
    TestLifecycle testLifecycle = new TestLifecycle(nullObserver);
    List<Observer> observers = new LinkedList<>();
    for (int i = 0; i < count; i++) {
        Observer observer = new Observer() {

            @Override
            public void update(Object args, Observable observable) {
            }
        };
        observers.add(observer);
        testLifecycle.addObserver(observer);
    }
    executors.execute(new Runnable() {

        @Override
        public void run() {
            observers.forEach(observer -> testLifecycle.removeObserver(observer));
        }
    });
    for (int i = 0; i < count * 10; i++) {
        testLifecycle.notifyObservers(new Object());
    }
    Assert.assertFalse(nullObserver.get());
}
Also used : AbstractLifecycleObservable(com.ctrip.xpipe.observer.AbstractLifecycleObservable) List(java.util.List) Observable(com.ctrip.xpipe.api.observer.Observable) Observer(com.ctrip.xpipe.api.observer.Observer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Assert(org.junit.Assert) LinkedList(java.util.LinkedList) AbstractTest(com.ctrip.xpipe.AbstractTest) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Observer(com.ctrip.xpipe.api.observer.Observer) LinkedList(java.util.LinkedList) AbstractLifecycleObservable(com.ctrip.xpipe.observer.AbstractLifecycleObservable) Observable(com.ctrip.xpipe.api.observer.Observable) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

Observable (com.ctrip.xpipe.api.observer.Observable)4 Observer (com.ctrip.xpipe.api.observer.Observer)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 AbstractTest (com.ctrip.xpipe.AbstractTest)1 AbstractLifecycleObservable (com.ctrip.xpipe.observer.AbstractLifecycleObservable)1 NodeAdded (com.ctrip.xpipe.observer.NodeAdded)1 AbstractConsoleTest (com.ctrip.xpipe.redis.console.AbstractConsoleTest)1 DefaultReplicationStore (com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStore)1 DefaultReplicationStoreManager (com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStoreManager)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Assert (org.junit.Assert)1