Search in sources :

Example 1 with LiveInstanceInfoProvider

use of org.apache.helix.LiveInstanceInfoProvider in project helix by apache.

the class ServiceDiscovery method register.

public boolean register(final String serviceId, final ServiceMetadata serviceMetadata) throws Exception {
    HelixManager helixManager = HelixManagerFactory.getZKHelixManager(cluster, serviceId, InstanceType.PARTICIPANT, zkAddress);
    LiveInstanceInfoProvider liveInstanceInfoProvider = new LiveInstanceInfoProvider() {

        @Override
        public ZNRecord getAdditionalLiveInstanceInfo() {
            // serialize serviceMetadata to ZNRecord
            ZNRecord rec = new ZNRecord(serviceId);
            rec.setSimpleField("HOST", serviceMetadata.getHost());
            rec.setSimpleField("PORT", String.valueOf(serviceMetadata.getPort()));
            rec.setSimpleField("SERVICE_NAME", serviceMetadata.getServiceName());
            return rec;
        }
    };
    helixManager.setLiveInstanceInfoProvider(liveInstanceInfoProvider);
    helixManager.connect();
    serviceMap.put(serviceId, helixManager);
    refreshCache();
    return true;
}
Also used : HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) LiveInstanceInfoProvider(org.apache.helix.LiveInstanceInfoProvider) ZNRecord(org.apache.helix.ZNRecord)

Example 2 with LiveInstanceInfoProvider

use of org.apache.helix.LiveInstanceInfoProvider in project helix by apache.

the class TestZkClusterManager method testLiveInstanceInfoProvider.

@Test
public void testLiveInstanceInfoProvider() throws Exception {
    System.out.println("START " + className + ".testLiveInstanceInfoProvider() at " + new Date(System.currentTimeMillis()));
    final String clusterName = CLUSTER_PREFIX + "_" + className + "_liveInstanceInfoProvider";
    class provider implements LiveInstanceInfoProvider {

        boolean _flag = false;

        public provider(boolean genSessionId) {
            _flag = genSessionId;
        }

        @Override
        public ZNRecord getAdditionalLiveInstanceInfo() {
            ZNRecord record = new ZNRecord("info");
            record.setSimpleField("simple", "value");
            List<String> listFieldVal = new ArrayList<String>();
            listFieldVal.add("val1");
            listFieldVal.add("val2");
            listFieldVal.add("val3");
            record.setListField("list", listFieldVal);
            Map<String, String> mapFieldVal = new HashMap<String, String>();
            mapFieldVal.put("k1", "val1");
            mapFieldVal.put("k2", "val2");
            mapFieldVal.put("k3", "val3");
            record.setMapField("map", mapFieldVal);
            if (_flag) {
                record.setSimpleField("SESSION_ID", "value");
                record.setSimpleField("LIVE_INSTANCE", "value");
                record.setSimpleField("Others", "value");
            }
            return record;
        }
    }
    TestHelper.setupEmptyCluster(_gZkClient, clusterName);
    int[] ids = { 0, 1, 2, 3, 4, 5 };
    setupInstances(clusterName, ids);
    // ///////////////////
    ZKHelixManager manager = new ZKHelixManager(clusterName, "localhost_0", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.connect();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_0"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 0);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 0);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 3);
    manager = new ZKHelixManager(clusterName, "localhost_1", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.setLiveInstanceInfoProvider(new provider(false));
    manager.connect();
    accessor = manager.getHelixDataAccessor();
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_1"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 4);
    manager = new ZKHelixManager(clusterName, "localhost_2", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.setLiveInstanceInfoProvider(new provider(true));
    manager.connect();
    accessor = manager.getHelixDataAccessor();
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_2"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
    Assert.assertFalse(liveInstance.getSessionId().equals("value"));
    Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
    // //////////////////////////////////
    MockParticipantManager manager2 = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_3");
    manager2.setLiveInstanceInfoProvider(new provider(true));
    manager2.connect();
    accessor = manager2.getHelixDataAccessor();
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_3"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
    Assert.assertFalse(liveInstance.getSessionId().equals("value"));
    Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
    String sessionId = liveInstance.getSessionId();
    ZkTestHelper.expireSession(manager2.getZkClient());
    Thread.sleep(1000);
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_3"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
    Assert.assertFalse(liveInstance.getSessionId().equals("value"));
    Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
    Assert.assertFalse(sessionId.equals(liveInstance.getSessionId()));
    System.out.println("END " + className + ".testLiveInstanceInfoProvider() at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HashMap(java.util.HashMap) LiveInstanceInfoProvider(org.apache.helix.LiveInstanceInfoProvider) ArrayList(java.util.ArrayList) Date(java.util.Date) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Aggregations

LiveInstanceInfoProvider (org.apache.helix.LiveInstanceInfoProvider)2 ZNRecord (org.apache.helix.ZNRecord)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixManager (org.apache.helix.HelixManager)1 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)1 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)1 LiveInstance (org.apache.helix.model.LiveInstance)1 Test (org.testng.annotations.Test)1