Java Code Examples for org.apache.zookeeper.Watcher.Event.EventType#None

The following examples show how to use org.apache.zookeeper.Watcher.Event.EventType#None . 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 check out the related API usage on the sidebar.
Example 1
Source File: ZkDistributedSemaphore.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private void onEvent(WatchedEvent event) {

    if(event.getType() == Watcher.Event.EventType.NodeChildrenChanged) {
      Collection<UpdateListener> col = new ArrayList<>(listeners.keySet());
      for(UpdateListener l : col) {
        l.updated();
      }
    } else if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected){
      // re set the watcher after a disconnect.
      try {
        setWatcher();
      } catch (Exception e) {
        logger.error("Failure while re-setting watcher after reconnect.", e);
      }
    }
  }
 
Example 2
Source File: ZooKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Command} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *         removal.
 */
public Watcher registerExpirationHandler(final ZooKeeperSessionExpireNotifier onExpired) {
    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
                try {
                    onExpired.notifySessionExpired();
                } catch (Exception exc) {
                    // do nothing
                }
            }
        }
    };
    register(watcher);
    return watcher;
}
 
Example 3
Source File: TestZooKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void expireZooKeeperSession(ZooKeeper zk, int timeout)
        throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }},
        zk.getSessionId(),
        zk.getSessionPasswd());

    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }

    newZk.close();
}
 
Example 4
Source File: ZookeeperRegistry.java    From Distributed-KV with Apache License 2.0 6 votes vote down vote up
@Override
public void process(WatchedEvent event) {
	// 传来NONE类型事件,一般是连接事件等
	if(event.getType() == EventType.None) {
		// 事件状态为:连接中
		if(event.getState() == KeeperState.SyncConnected) {
			// 唤醒等待连接成功的线程
			mutex.lock();
			try {
				// 唤醒等待连接成功的线程
				connCondition.signalAll();
			} finally {
				mutex.unlock();
			}
		}
	}
	
}
 
Example 5
Source File: ZooKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Command} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *         removal.
 */
public Watcher registerExpirationHandler(final ZooKeeperSessionExpireNotifier onExpired) {
    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
                try {
                    onExpired.notifySessionExpired();
                } catch (Exception exc) {
                    // do nothing
                }
            }
        }
    };
    register(watcher);
    return watcher;
}
 
Example 6
Source File: TestZooKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void expireZooKeeperSession(ZooKeeper zk, int timeout)
        throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }},
        zk.getSessionId(),
        zk.getSessionPasswd());

    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }

    newZk.close();
}
 
Example 7
Source File: LeaderElection.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    if (stopped) {
        return;
    }

    if (event.getType() == EventType.None &&
            (event.getState().equals(Event.KeeperState.Disconnected) ||
            event.getState().equals(Event.KeeperState.Expired))) {

        if (elected) {
            elected = false;
            log.info("No longer leader for the position of " + position);
            leaderProvisioner.setRequiredState(LeaderState.I_AM_NOT_LEADER);
        }

        // Note that if we get a disconnected event here, our watcher is not unregistered, thus we will
        // get a connected event on this watcher once we are reconnected.

        // Since we are not owner of the ZooKeeper handle, we assume Expired states are handled
        // elsewhere in the application.

    } else if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
        // Upon reconnect, since our session was not expired, our ephemeral node will still
        // exist (it might even be the leader), therefore have a look at the leaders.
        watchLeaders();
    }
}
 
Example 8
Source File: ZkTestHelper.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Simulate a zk state change by calling {@link ZkClient#process(WatchedEvent)} directly
 */
public static void simulateZkStateReconnected(RealmAwareZkClient client) {
  ZkClient zkClient = (ZkClient) client;
  WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Disconnected, null);
  zkClient.process(event);
  event = new WatchedEvent(EventType.None, KeeperState.SyncConnected, null);
  zkClient.process(event);
}
 
Example 9
Source File: ZkTestHelper.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Simulate a zk state change by calling {@link ZkClient#process(WatchedEvent)} directly
 */
public static void simulateZkStateReconnected(RealmAwareZkClient client) {
  ZkClient zkClient = (ZkClient) client;
  WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Disconnected, null);
  zkClient.process(event);
  event = new WatchedEvent(EventType.None, KeeperState.SyncConnected, null);
  zkClient.process(event);
}
 
Example 10
Source File: TestPersistentEphemeralNode.java    From curator with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event)
{
    if ( types.contains(event.getType()) )
    {
        latch.countDown();
    }
    else if ( event.getType() != EventType.None )
    {
        log.warn("Unexpected watcher event: " + event);
    }
}
 
Example 11
Source File: ZooKeeperClientTest.java    From Mario with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
	System.err.println("MyWatcher " + event.getType() + " | "
			+ event.getState());
	if (event.getType() == EventType.None) {
		if (event.getState().equals(KeeperState.SyncConnected)) {
			coutCountDownLatch.countDown();
		}
	}
}
 
Example 12
Source File: ZooKeeperClient.java    From Mario with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    logger.info("SessionWatcher: " + connectionString + "|"
                + event.getType() + "|" + event.getState());
    if (event.getType() == EventType.None) {
        if (event.getState().equals(KeeperState.SyncConnected)) {
            isAvailable = true;
            countDownLatch.countDown();
        } else if (event.getState().equals(KeeperState.Expired)) {
            createConnection();
        } else if (event.getState().equals(KeeperState.Disconnected)) {
            isAvailable = false;
        }
    }
}
 
Example 13
Source File: ZookeeperWatcher.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) throws Exception {
    String path = event.getPath();
    EventType type = event.getType();

    if (type != EventType.None) {
        usingWatcher(watcherType, path);
    }

    switch (type) {
        case None:
            none(event);
            break;
        case NodeCreated:
            nodeCreated(event);
            break;
        case NodeDeleted:
            nodeDeleted(event);
            break;
        case NodeDataChanged:
            nodeDataChanged(event);
            break;
        case NodeChildrenChanged:
            nodeChildrenChanged(event);
            break;
    }
}
 
Example 14
Source File: ZookeeperBBoxDBInstanceAdapter.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Process zooekeeper events
 * 
 * @param watchedEvent
 */
private synchronized void processZookeeperEvent(final WatchedEvent watchedEvent) {
	// Ignore null parameter
	if (watchedEvent == null) {
		logger.warn("process called with an null argument");
		return;
	}

	final ServiceState serviceState = zookeeperClient.getServiceState();
	
	// Shutdown is pending, stop event processing
	if (! serviceState.isInRunningState()) {
		logger.debug("Ignoring event {}, because service state is {}", watchedEvent, serviceState);
		return;
	}

	// Ignore type=none event
	if (watchedEvent.getType() == EventType.None) {
		return;
	}

	// Process events
	if (watchedEvent.getPath() != null) {
		readMembershipAndRegisterWatch();	
	} else {
		logger.warn("Got unknown zookeeper event: {}", watchedEvent);
	}
}
 
Example 15
Source File: ZooKeeperSessionWatcherTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess2() {
    WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Disconnected, null);
    sessionWatcher.process(event);
    assertFalse(sessionWatcher.isShutdownStarted());
    assertEquals(shutdownService.getExitCode(), 0);
}
 
Example 16
Source File: ZooKeeperSessionWatcherTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess1() {
    WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Expired, null);
    sessionWatcher.process(event);
    assertTrue(sessionWatcher.isShutdownStarted());
    assertEquals(shutdownService.getExitCode(), -1);
}
 
Example 17
Source File: TestZooKeeperClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private CountDownLatch awaitConnectionEvent(final KeeperState state, final ZooKeeperClient zkc) {
    final CountDownLatch connected = new CountDownLatch(1);
    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == state) {
                connected.countDown();
            }
        }
    };
    zkc.register(watcher);
    return connected;
}
 
Example 18
Source File: TestZooKeeperClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private CountDownLatch awaitConnectionEvent(final KeeperState state, final ZooKeeperClient zkc) {
    final CountDownLatch connected = new CountDownLatch(1);
    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == state) {
                connected.countDown();
            }
        }
    };
    zkc.register(watcher);
    return connected;
}