Search in sources :

Example 1 with DbusPartitionInfo

use of com.linkedin.databus.client.pub.DbusPartitionInfo in project databus by linkedin.

the class ClientStateRequestProcessor method getV2PartitionRegistration.

/**
   * Helper method to get partition registration information for a given V2 Cluster
   * partition
   * 
   * @param cluster
   *          V2 Cluster
   * @param partition
   *          Partition in the cluster.
   * @return
   * @throws RequestProcessingException
   *           When cluster or partition is not hosted in this instance.
   */
private RegInfo getV2PartitionRegistration(String cluster, long partition) throws RequestProcessingException {
    DatabusV2ClusterRegistrationImpl reg = getV2ClusterRegistration(cluster);
    DbusPartitionInfo p = new DbusPartitionInfoImpl(partition);
    DatabusRegistration r = reg.getPartitionRegs().get(p);
    if (null == r)
        throw new RequestProcessingException("Partition(" + partition + ") for cluster (" + cluster + ") not found !!");
    return new RegInfo(r.getState().name(), r.getRegistrationId(), r.getStatus(), r.getFilterConfig(), r.getSubscriptions());
}
Also used : DatabusV2ClusterRegistrationImpl(com.linkedin.databus.client.registration.DatabusV2ClusterRegistrationImpl) DatabusRegistration(com.linkedin.databus.client.pub.DatabusRegistration) DbusPartitionInfoImpl(com.linkedin.databus.client.DbusPartitionInfoImpl) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) DbusPartitionInfo(com.linkedin.databus.client.pub.DbusPartitionInfo)

Example 2 with DbusPartitionInfo

use of com.linkedin.databus.client.pub.DbusPartitionInfo in project databus by linkedin.

the class ClientStateRequestProcessor method findV2Registration.

/**
   * Helper method to locate a databus V2 registration by its registration id. This method
   * can locate both top-level (registered by one of _dbusClient.registerXXX()) and
   * individual-partition (child) registration that are aggregated inside a top-level
   * MultiPartition registration.
   * 
   * Please note that this can traverse the registration tree which is 1 level deep. In
   * other words, it will not work when we have MultiPartition registrations aggregated
   * inside another MultiPartition registrations.
   * 
   * @param regId
   *          Registration Id to be located
   * @param request
   *          Databus Request corresponding to the REST call.
   * @return
   * @throws RequestProcessingException
   *           when the registration is not found.
   */
private DatabusRegistration findV2Registration(DatabusRequest request, String prefix) throws RequestProcessingException {
    String category = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
    String registrationIdStr = category.substring(prefix.length());
    RegistrationId regId = new RegistrationId(registrationIdStr);
    Collection<DatabusRegistration> regs = _client.getAllRegistrations();
    if (null != regs) {
        for (DatabusRegistration r : regs) {
            if (regId.equals(r.getRegistrationId())) {
                return r;
            }
            /**
         * Important Note: There is an important implementation difference on which
         * registrations are stored in the global registration data-structure maintained
         * by the client (DatabusHttp[V3]ClientImpls) between V2 and V3.
         * 
         * 1. In the case of V2, only top-level registrations are stored in the global
         * data-structure (DatabusHttpClientImpl.regList 2. In the case of V3, all
         * registrations are stored in the global data-structure.
         * 
         * In the case of V3, this is needed so that all registrations can act on the
         * relay external view change. This can be refactored in the future by moving the
         * relay-external view change to registration impl ( reduce the complexity in
         * ClientImpl ). The V2 implementation did not have this logic and was following a
         * more intuitive structure of preserving the hierarchy. The below code handles
         * the discrepancy for V2.
         */
            if (r instanceof DatabusMultiPartitionRegistration) {
                Map<DbusPartitionInfo, DatabusRegistration> childRegs = ((DatabusMultiPartitionRegistration) r).getPartitionRegs();
                for (Entry<DbusPartitionInfo, DatabusRegistration> e : childRegs.entrySet()) {
                    if (regId.equals(e.getValue().getRegistrationId())) {
                        return e.getValue();
                    }
                }
            }
        }
    }
    throw new RequestProcessingException("Unable to find registration (" + regId + ") ");
}
Also used : DatabusRegistration(com.linkedin.databus.client.pub.DatabusRegistration) DatabusMultiPartitionRegistration(com.linkedin.databus.client.registration.DatabusMultiPartitionRegistration) RegistrationId(com.linkedin.databus.client.pub.RegistrationId) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) DbusPartitionInfo(com.linkedin.databus.client.pub.DbusPartitionInfo)

Example 3 with DbusPartitionInfo

use of com.linkedin.databus.client.pub.DbusPartitionInfo in project databus by linkedin.

the class ClientStateRequestProcessor method getV2ClusterPartitions.

/**
   * Get the list of partitions hosted by this client for the V2 cluster.
   * 
   * @param cluster
   *          V2 CLuster for which we need to find out the partitions.
   * @return
   * @throws RequestProcessingException
   *           when unable to find the cluster.
   */
private Collection<PartitionInfo> getV2ClusterPartitions(String cluster) throws RequestProcessingException {
    DatabusV2ClusterRegistrationImpl reg = getV2ClusterRegistration(cluster);
    List<PartitionInfo> partitions = new ArrayList<PartitionInfo>();
    Map<DbusPartitionInfo, DatabusRegistration> regMap = reg.getPartitionRegs();
    for (Entry<DbusPartitionInfo, DatabusRegistration> e : regMap.entrySet()) {
        PartitionInfo p = new PartitionInfo(e.getKey().getPartitionId(), e.getValue().getRegistrationId());
        partitions.add(p);
    }
    return partitions;
}
Also used : DatabusV2ClusterRegistrationImpl(com.linkedin.databus.client.registration.DatabusV2ClusterRegistrationImpl) DatabusRegistration(com.linkedin.databus.client.pub.DatabusRegistration) ArrayList(java.util.ArrayList) DbusPartitionInfo(com.linkedin.databus.client.pub.DbusPartitionInfo) DbusPartitionInfo(com.linkedin.databus.client.pub.DbusPartitionInfo)

Example 4 with DbusPartitionInfo

use of com.linkedin.databus.client.pub.DbusPartitionInfo in project databus by linkedin.

the class ClientStateRequestProcessor method getAllTopLevelV2Registrations.

/**
   * Returns all the top-level V2 registrations. Top-level registrations are those that
   * were created as a result of one of "registerXXX()" calls on databus-client. In the
   * case of multi-partition registrations (like V2 CLB), only the parent registration is
   * considered the top-level registration. Per-partition (child) registrations which were
   * created as part of partition migration are NOT top-level registrations.
   * 
   * @return collection of top-level registrations (V2)
   */
private Collection<RegInfo> getAllTopLevelV2Registrations() {
    List<RegInfo> regList = new ArrayList<RegInfo>();
    Collection<DatabusRegistration> regs = _client.getAllRegistrations();
    for (DatabusRegistration r : regs) {
        RegInfo regInfo = null;
        if (r instanceof DatabusMultiPartitionRegistration) {
            Map<DbusPartitionInfo, DatabusRegistration> childRegs = ((DatabusMultiPartitionRegistration) r).getPartitionRegs();
            Map<DbusPartitionInfo, RegInfo> childR = new HashMap<DbusPartitionInfo, RegInfo>();
            for (Entry<DbusPartitionInfo, DatabusRegistration> e : childRegs.entrySet()) {
                childR.put(e.getKey(), new RegInfo(e.getValue().getState().name(), e.getValue().getRegistrationId(), e.getValue().getStatus(), e.getValue().getFilterConfig(), e.getValue().getSubscriptions()));
            }
            regInfo = new RegInfo(r.getState().name(), r.getRegistrationId(), r.getStatus(), r.getFilterConfig(), r.getSubscriptions(), true, childR);
        } else {
            regInfo = new RegInfo(r.getState().name(), r.getRegistrationId(), r.getStatus(), r.getFilterConfig(), r.getSubscriptions());
        }
        regList.add(regInfo);
    }
    return regList;
}
Also used : DatabusRegistration(com.linkedin.databus.client.pub.DatabusRegistration) DatabusMultiPartitionRegistration(com.linkedin.databus.client.registration.DatabusMultiPartitionRegistration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DbusPartitionInfo(com.linkedin.databus.client.pub.DbusPartitionInfo)

Example 5 with DbusPartitionInfo

use of com.linkedin.databus.client.pub.DbusPartitionInfo in project databus by linkedin.

the class TestDatabusV2ClusterRegistrationImpl method testRegistration.

@Test
public void testRegistration() throws Exception {
    DatabusHttpClientImpl client = null;
    try {
        DatabusHttpClientImpl.Config clientConfig = new DatabusHttpClientImpl.Config();
        clientConfig.getContainer().getJmx().setRmiEnabled(false);
        clientConfig.getContainer().setHttpPort(12003);
        client = new DatabusHttpClientImpl(clientConfig);
        registerRelay(1, "relay1", new InetSocketAddress("localhost", 8888), "S1,S2", client);
        registerRelay(2, "relay2", new InetSocketAddress("localhost", 7777), "S1,S3", client);
        registerRelay(3, "relay1.1", new InetSocketAddress("localhost", 8887), "S1,S2", client);
        registerRelay(4, "relay3", new InetSocketAddress("localhost", 6666), "S3,S4,S5", client);
        TestDbusPartitionListener listener = new TestDbusPartitionListener();
        StaticConfig ckptConfig = new StaticConfig("localhost:1356", "dummy", 1, 1);
        DbusClusterInfo clusterInfo = new DbusClusterInfo("dummy", 10, 1);
        DatabusV2ClusterRegistrationImpl reg = new TestableDatabusV2ClusterRegistrationImpl(null, client, ckptConfig, clusterInfo, new TestDbusClusterConsumerFactory(), new TestDbusServerSideFilterFactory(), listener, "S1", "S3");
        reg.onRegister();
        // Start
        reg.start();
        assertEquals("State CHeck", reg.getState(), RegistrationState.STARTED);
        // Add Partition(s)
        reg.onGainedPartitionOwnership(1);
        assertEquals("Listener called ", listener.isAddPartitionCalled(1), true);
        reg.onGainedPartitionOwnership(2);
        assertEquals("Listener called ", listener.isAddPartitionCalled(2), true);
        assertEquals("Partition Regs size ", 2, reg.getPartitionRegs().size());
        reg.onGainedPartitionOwnership(3);
        assertEquals("Listener called ", listener.isAddPartitionCalled(3), true);
        assertEquals("Partition Regs size ", 3, reg.getPartitionRegs().size());
        reg.onGainedPartitionOwnership(4);
        assertEquals("Listener called ", listener.isAddPartitionCalled(4), true);
        //duplicate call
        listener.clearCallbacks();
        reg.onGainedPartitionOwnership(4);
        assertEquals("Listener called ", listener.isAddPartitionCalled(4), false);
        assertEquals("Partition Regs size ", 4, reg.getPartitionRegs().size());
        List<String> gotPartitionList = new ArrayList<String>();
        for (DbusPartitionInfo p : reg.getPartitions()) gotPartitionList.add(p.toString());
        Collections.sort(gotPartitionList);
        assertEquals("Partitions Check", gotPartitionList.toString(), "[1, 2, 3, 4]");
        // Drop Partitions
        reg.onLostPartitionOwnership(1);
        gotPartitionList.clear();
        for (DbusPartitionInfo p : reg.getPartitions()) gotPartitionList.add(p.toString());
        Collections.sort(gotPartitionList);
        assertEquals("Partitions Check", "[2, 3, 4]", gotPartitionList.toString());
        assertEquals("Listener called ", true, listener.isDropPartitionCalled(1));
        reg.onLostPartitionOwnership(2);
        assertEquals("Listener called ", true, listener.isDropPartitionCalled(2));
        //duplicate call
        listener.clearCallbacks();
        reg.onLostPartitionOwnership(2);
        assertEquals("Listener called ", false, listener.isDropPartitionCalled(2));
        assertEquals("Partitions Check", "[3, 4]", reg.getPartitions().toString());
        assertEquals("Partition Regs size ", 2, reg.getPartitionRegs().size());
        reg.onReset(3);
        assertEquals("Listener called ", true, listener.isDropPartitionCalled(3));
        //duplicate call
        listener.clearCallbacks();
        reg.onReset(3);
        assertEquals("Listener called ", false, listener.isDropPartitionCalled(3));
        assertEquals("Partitions Check", "[4]", reg.getPartitions().toString());
        assertEquals("Partition Regs size ", 1, reg.getPartitionRegs().size());
        reg.onError(4);
        assertEquals("Listener called ", true, listener.isDropPartitionCalled(4));
        //duplicate call
        listener.clearCallbacks();
        reg.onError(4);
        assertEquals("Listener called ", false, listener.isDropPartitionCalled(4));
        assertEquals("Partitions Check", "[]", reg.getPartitions().toString());
        assertEquals("Partition Regs size ", 0, reg.getPartitionRegs().size());
        // Add Partiton 1 again
        listener.clearCallbacks();
        reg.onGainedPartitionOwnership(1);
        assertEquals("Listener called ", listener.isAddPartitionCalled(1), true);
        assertEquals("Partition Regs size ", 1, reg.getPartitionRegs().size());
        assertEquals("Child State CHeck", reg.getPartitionRegs().values().iterator().next().getState(), RegistrationState.STARTED);
        // Pausing
        reg.pause();
        assertEquals("State CHeck", reg.getState(), RegistrationState.PAUSED);
        assertEquals("Child State CHeck", reg.getPartitionRegs().values().iterator().next().getState(), RegistrationState.PAUSED);
        // Resume
        reg.resume();
        assertEquals("State CHeck", reg.getState(), RegistrationState.RESUMED);
        assertEquals("Child State CHeck", reg.getPartitionRegs().values().iterator().next().getState(), RegistrationState.RESUMED);
        // Suspended
        reg.suspendOnError(null);
        assertEquals("State CHeck", reg.getState(), RegistrationState.SUSPENDED_ON_ERROR);
        assertEquals("Child State CHeck", reg.getPartitionRegs().values().iterator().next().getState(), RegistrationState.SUSPENDED_ON_ERROR);
        // resume
        reg.resume();
        assertEquals("State CHeck", reg.getState(), RegistrationState.RESUMED);
        assertEquals("Child State CHeck", reg.getPartitionRegs().values().iterator().next().getState(), RegistrationState.RESUMED);
        // Active node change notification
        List<String> newActiveNodes = new ArrayList<String>();
        newActiveNodes.add("localhost:7070");
        newActiveNodes.add("localhost:8080");
        newActiveNodes.add("localhost:9090");
        reg.onInstanceChange(newActiveNodes);
        assertEquals("Active Nodes", newActiveNodes, reg.getCurrentActiveNodes());
        newActiveNodes.remove(2);
        reg.onInstanceChange(newActiveNodes);
        assertEquals("Active Nodes", newActiveNodes, reg.getCurrentActiveNodes());
        newActiveNodes.add("localhost:1010");
        reg.onInstanceChange(newActiveNodes);
        assertEquals("Active Nodes", newActiveNodes, reg.getCurrentActiveNodes());
        // Partition Mapping change notification
        Map<Integer, String> activePartitionMap = new HashMap<Integer, String>();
        for (int i = 0; i < 10; i++) {
            String node = null;
            if (i % 2 == 0)
                node = "localhost:8080";
            else
                node = "localhost:7070";
            activePartitionMap.put(i, node);
        }
        reg.onPartitionMappingChange(activePartitionMap);
        assertEquals("Partition Mapping Check", activePartitionMap, reg.getActivePartitionMap());
        activePartitionMap.remove(9);
        reg.onPartitionMappingChange(activePartitionMap);
        assertEquals("Partition Mapping Check", activePartitionMap, reg.getActivePartitionMap());
        activePartitionMap.put(9, "localhost:8708");
        reg.onPartitionMappingChange(activePartitionMap);
        assertEquals("Partition Mapping Check", activePartitionMap, reg.getActivePartitionMap());
        // shutdown
        reg.shutdown();
        assertEquals("State Check", reg.getState(), RegistrationState.SHUTDOWN);
        assertEquals("Child State CHeck", reg.getPartitionRegs().values().iterator().next().getState(), RegistrationState.SHUTDOWN);
        // Operations during shutdown state
        boolean gotException = false;
        try {
            reg.onGainedPartitionOwnership(1);
        } catch (IllegalStateException ex) {
            gotException = true;
        }
        assertEquals("Exception", true, gotException);
        gotException = false;
        try {
            reg.onLostPartitionOwnership(1);
        } catch (IllegalStateException ex) {
            gotException = true;
        }
        assertEquals("Exception", true, gotException);
        gotException = false;
        try {
            reg.pause();
        } catch (IllegalStateException ex) {
            gotException = true;
        }
        assertEquals("Exception", true, gotException);
        gotException = false;
        try {
            reg.suspendOnError(null);
        } catch (IllegalStateException ex) {
            gotException = true;
        }
        assertEquals("Exception", true, gotException);
        gotException = false;
        try {
            reg.resume();
        } catch (IllegalStateException ex) {
            gotException = true;
        }
        assertEquals("Exception", true, gotException);
        // deregister
        reg.deregister();
        assertEquals("State Check", reg.getState(), RegistrationState.DEREGISTERED);
        assertEquals("Child State CHeck", 0, reg.getPartitionRegs().size());
    } finally {
        if (null != client)
            client.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) DbusKeyCompositeFilterConfig(com.linkedin.databus2.core.filter.DbusKeyCompositeFilterConfig) StaticConfig(com.linkedin.databus.client.pub.ClusterCheckpointPersistenceProvider.StaticConfig) InetSocketAddress(java.net.InetSocketAddress) StaticConfig(com.linkedin.databus.client.pub.ClusterCheckpointPersistenceProvider.StaticConfig) ArrayList(java.util.ArrayList) DatabusHttpClientImpl(com.linkedin.databus.client.DatabusHttpClientImpl) DbusClusterInfo(com.linkedin.databus.client.pub.DbusClusterInfo) DbusPartitionInfo(com.linkedin.databus.client.pub.DbusPartitionInfo) Test(org.testng.annotations.Test)

Aggregations

DbusPartitionInfo (com.linkedin.databus.client.pub.DbusPartitionInfo)10 DatabusRegistration (com.linkedin.databus.client.pub.DatabusRegistration)5 DbusPartitionInfoImpl (com.linkedin.databus.client.DbusPartitionInfoImpl)4 RegistrationId (com.linkedin.databus.client.pub.RegistrationId)3 DatabusMultiPartitionRegistration (com.linkedin.databus.client.registration.DatabusMultiPartitionRegistration)3 RequestProcessingException (com.linkedin.databus2.core.container.request.RequestProcessingException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DbusClusterInfo (com.linkedin.databus.client.pub.DbusClusterInfo)2 DatabusV2ClusterRegistrationImpl (com.linkedin.databus.client.registration.DatabusV2ClusterRegistrationImpl)2 DatabusHttpClientImpl (com.linkedin.databus.client.DatabusHttpClientImpl)1 StaticConfig (com.linkedin.databus.client.pub.ClusterCheckpointPersistenceProvider.StaticConfig)1 DatabusClientException (com.linkedin.databus.client.pub.DatabusClientException)1 DatabusV3MultiPartitionRegistration (com.linkedin.databus.client.pub.DatabusV3MultiPartitionRegistration)1 DatabusV3Registration (com.linkedin.databus.client.pub.DatabusV3Registration)1 DbusModPartitionedFilterFactory (com.linkedin.databus.client.pub.DbusModPartitionedFilterFactory)1 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)1 DatabusException (com.linkedin.databus2.core.DatabusException)1 DbusKeyCompositeFilterConfig (com.linkedin.databus2.core.filter.DbusKeyCompositeFilterConfig)1 InetSocketAddress (java.net.InetSocketAddress)1