Search in sources :

Example 51 with ZNRecordSerializer

use of org.apache.helix.manager.zk.ZNRecordSerializer in project helix by apache.

the class HelixAdminWebApp method start.

public synchronized void start() throws Exception {
    LOG.info("helixAdminWebApp starting");
    if (_component == null) {
        _zkClient = new ZkClient(_zkServerAddress, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
        _rawZkClient = new ZkClient(_zkServerAddress, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
        _component = new Component();
        _component.getServers().add(Protocol.HTTP, _helixAdminPort);
        Context applicationContext = _component.getContext().createChildContext();
        applicationContext.getAttributes().put(RestAdminApplication.ZKSERVERADDRESS, _zkServerAddress);
        applicationContext.getAttributes().put(RestAdminApplication.PORT, "" + _helixAdminPort);
        applicationContext.getAttributes().put(RestAdminApplication.ZKCLIENT, _zkClient);
        applicationContext.getAttributes().put(ResourceUtil.ContextKey.RAW_ZKCLIENT.toString(), _rawZkClient);
        _adminApp = new RestAdminApplication(applicationContext);
        // Attach the application to the component and start it
        _component.getDefaultHost().attach(_adminApp);
        _component.start();
    }
    LOG.info("helixAdminWebApp started on port: " + _helixAdminPort);
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Context(org.restlet.Context) Component(org.restlet.Component) ByteArraySerializer(org.apache.helix.manager.zk.ByteArraySerializer) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 52 with ZNRecordSerializer

use of org.apache.helix.manager.zk.ZNRecordSerializer in project helix by apache.

the class ClusterRepresentationUtil method getInstancePropertiesAsString.

public static String getInstancePropertiesAsString(ZkClient zkClient, String clusterName, PropertyKey propertyKey, MediaType mediaType) throws JsonGenerationException, JsonMappingException, IOException {
    zkClient.setZkSerializer(new ZNRecordSerializer());
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
    List<ZNRecord> records = HelixProperty.convertToList(accessor.getChildValues(propertyKey));
    return ObjectToJson(records);
}
Also used : ZNRecord(org.apache.helix.ZNRecord) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 53 with ZNRecordSerializer

use of org.apache.helix.manager.zk.ZNRecordSerializer in project helix by apache.

the class Worker method run.

@Override
public void run() {
    ZkClient zkclient = null;
    try {
        // add node to cluster if not already added
        zkclient = new ZkClient(_zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
        ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
        List<String> nodes = admin.getInstancesInCluster(_clusterName);
        if (!nodes.contains(_instanceName)) {
            InstanceConfig config = new InstanceConfig(_instanceName);
            config.setHostName("localhost");
            config.setInstanceEnabled(true);
            admin.addInstance(_clusterName, config);
        }
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                System.out.println("Shutting down " + _instanceName);
                disconnect();
            }
        });
        connect();
    } finally {
        if (zkclient != null) {
            zkclient.close();
        }
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 54 with ZNRecordSerializer

use of org.apache.helix.manager.zk.ZNRecordSerializer in project helix by apache.

the class IdealStateBuilderExample method main.

public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println("USAGE: java IdealStateBuilderExample zkAddress clusterName idealStateMode" + " (FULL_AUTO, SEMI_AUTO, CUSTOMIZED, or USER_DEFINED)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = args[1];
    RebalanceMode idealStateMode = RebalanceMode.valueOf(args[2].toUpperCase());
    ZkClient zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
    // add cluster
    admin.addCluster(clusterName, true);
    // add MasterSlave state model definition
    admin.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave()));
    // add 2 participants: "localhost:{12918, 12919}"
    int n = 2;
    for (int i = 0; i < n; i++) {
        int port = 12918 + i;
        InstanceConfig config = new InstanceConfig("localhost_" + port);
        config.setHostName("localhost");
        config.setPort(Integer.toString(port));
        config.setInstanceEnabled(true);
        admin.addInstance(clusterName, config);
    }
    // add ideal-state according to ideal-state-mode
    String resourceName = "TestDB";
    IdealState idealState = null;
    switch(idealStateMode) {
        case SEMI_AUTO:
            {
                SemiAutoModeISBuilder builder = new SemiAutoModeISBuilder(resourceName);
                builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2);
                builder.assignPreferenceList(buildPartitionName(resourceName, 0), "localhost_12918", "localhost_12919").assignPreferenceList(buildPartitionName(resourceName, 1), "localhost_12919", "localhost_12918");
                idealState = builder.build();
                break;
            }
        case FULL_AUTO:
            {
                FullAutoModeISBuilder builder = new FullAutoModeISBuilder(resourceName);
                builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2).setMaxPartitionsPerNode(2);
                builder.add(buildPartitionName(resourceName, 0)).add(buildPartitionName(resourceName, 1));
                idealState = builder.build();
                break;
            }
        case CUSTOMIZED:
            {
                CustomModeISBuilder builder = new CustomModeISBuilder(resourceName);
                builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2);
                builder.assignInstanceAndState(buildPartitionName(resourceName, 0), "localhost_12918", "MASTER").assignInstanceAndState(buildPartitionName(resourceName, 0), "localhost_12919", "SLAVE").assignInstanceAndState(buildPartitionName(resourceName, 1), "localhost_12918", "SLAVE").assignInstanceAndState(buildPartitionName(resourceName, 1), "localhost_12919", "MASTER");
                idealState = builder.build();
                break;
            }
        default:
            break;
    }
    admin.addResource(clusterName, resourceName, idealState);
    // start helix controller
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                HelixControllerMain.main(new String[] { "--zkSvr", zkAddr, "--cluster", clusterName });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();
    // start dummy participants
    for (int i = 0; i < n; i++) {
        int port = 12918 + i;
        final String instanceName = "localhost_" + port;
        new Thread(new Runnable() {

            @Override
            public void run() {
                DummyParticipant.main(new String[] { zkAddr, clusterName, instanceName });
            }
        }).start();
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) CustomModeISBuilder(org.apache.helix.model.builder.CustomModeISBuilder) SemiAutoModeISBuilder(org.apache.helix.model.builder.SemiAutoModeISBuilder) FullAutoModeISBuilder(org.apache.helix.model.builder.FullAutoModeISBuilder) IdealState(org.apache.helix.model.IdealState) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) StateModelDefinition(org.apache.helix.model.StateModelDefinition) RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 55 with ZNRecordSerializer

use of org.apache.helix.manager.zk.ZNRecordSerializer in project helix by apache.

the class TestJacksonPayloadSerializer method testFullZNRecordSerializeDeserialize.

/**
 * Test that the payload can be deserialized after serializing and deserializing the ZNRecord
 * that encloses it. This uses ZNRecordSerializer.
 */
@Test
public void testFullZNRecordSerializeDeserialize() {
    final String RECORD_ID = "testFullZNRecordSerializeDeserialize";
    SampleDeserialized sample = getSample();
    ZNRecord znRecord = new ZNRecord(RECORD_ID);
    znRecord.setPayloadSerializer(new JacksonPayloadSerializer());
    znRecord.setPayload(sample);
    ZNRecordSerializer znRecordSerializer = new ZNRecordSerializer();
    byte[] serialized = znRecordSerializer.serialize(znRecord);
    ZNRecord deserialized = (ZNRecord) znRecordSerializer.deserialize(serialized);
    deserialized.setPayloadSerializer(new JacksonPayloadSerializer());
    SampleDeserialized duplicate = deserialized.getPayload(SampleDeserialized.class);
    Assert.assertEquals(duplicate, sample);
}
Also used : ZNRecord(org.apache.helix.ZNRecord) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) Test(org.testng.annotations.Test)

Aggregations

ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)59 ZkClient (org.apache.helix.manager.zk.ZkClient)39 ZNRecord (org.apache.helix.ZNRecord)23 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 Test (org.testng.annotations.Test)11 IdealState (org.apache.helix.model.IdealState)10 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)9 Date (java.util.Date)7 InstanceConfig (org.apache.helix.model.InstanceConfig)7 ClusterSetup (org.apache.helix.tools.ClusterSetup)7 Builder (org.apache.helix.PropertyKey.Builder)6 StateModelDefinition (org.apache.helix.model.StateModelDefinition)6 HelixDataAccessor (org.apache.helix.HelixDataAccessor)5 ExternalView (org.apache.helix.model.ExternalView)5 PropertyKey (org.apache.helix.PropertyKey)4 BeforeClass (org.testng.annotations.BeforeClass)4 BeforeSuite (org.testng.annotations.BeforeSuite)4 File (java.io.File)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3