Search in sources :

Example 1 with InstanceZKMetadata

use of com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata in project pinot by linkedin.

the class InstanceZKMetadataTest method getTestInstanceMetadata.

private InstanceZKMetadata getTestInstanceMetadata() {
    InstanceZKMetadata instanceMetadata = new InstanceZKMetadata();
    instanceMetadata.setInstanceType("Server");
    instanceMetadata.setInstanceName("lva1-app0120.corp.linkedin.com");
    instanceMetadata.setInstancePort(8001);
    for (int i = 0; i < 10; ++i) {
        instanceMetadata.setGroupId("testRes" + i, "groupId" + i);
        instanceMetadata.setPartition("testRes" + i, "part" + i);
    }
    return instanceMetadata;
}
Also used : InstanceZKMetadata(com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata)

Example 2 with InstanceZKMetadata

use of com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata in project pinot by linkedin.

the class PinotRealtimeSegmentManager method assignRealtimeSegmentsToServerInstancesIfNecessary.

private synchronized void assignRealtimeSegmentsToServerInstancesIfNecessary() throws JSONException, IOException {
    // Fetch current ideal state snapshot
    Map<String, IdealState> idealStateMap = new HashMap<String, IdealState>();
    for (String resource : _pinotHelixResourceManager.getAllRealtimeTables()) {
        final String tableName = TableNameBuilder.extractRawTableName(resource);
        AbstractTableConfig tableConfig = _pinotHelixResourceManager.getTableConfig(tableName, TableType.REALTIME);
        KafkaStreamMetadata metadata = new KafkaStreamMetadata(tableConfig.getIndexingConfig().getStreamConfigs());
        if (metadata.hasHighLevelKafkaConsumerType()) {
            idealStateMap.put(resource, _pinotHelixResourceManager.getHelixAdmin().getResourceIdealState(_pinotHelixResourceManager.getHelixClusterName(), resource));
        } else {
            LOGGER.debug("Not considering table {} for realtime segment assignment");
        }
    }
    List<Pair<String, String>> listOfSegmentsToAddToInstances = new ArrayList<Pair<String, String>>();
    for (String resource : idealStateMap.keySet()) {
        try {
            IdealState state = idealStateMap.get(resource);
            // Are there any partitions?
            if (state.getPartitionSet().size() == 0) {
                // No, this is a brand new ideal state, so we will add one new segment to every partition and replica
                List<String> instancesInResource = new ArrayList<String>();
                try {
                    instancesInResource.addAll(_pinotHelixResourceManager.getServerInstancesForTable(resource, TableType.REALTIME));
                } catch (Exception e) {
                    LOGGER.error("Caught exception while fetching instances for resource {}", resource, e);
                    _controllerMetrics.addMeteredGlobalValue(ControllerMeter.CONTROLLER_REALTIME_TABLE_SEGMENT_ASSIGNMENT_ERROR, 1L);
                }
                // Assign a new segment to all server instances
                for (String instanceId : instancesInResource) {
                    InstanceZKMetadata instanceZKMetadata = _pinotHelixResourceManager.getInstanceZKMetadata(instanceId);
                    if (instanceZKMetadata == null) {
                        LOGGER.warn("Instance {} has no associated instance metadata in ZK, ignoring for segment assignment.", instanceId);
                        _controllerMetrics.addMeteredGlobalValue(ControllerMeter.CONTROLLER_REALTIME_TABLE_SEGMENT_ASSIGNMENT_ERROR, 1L);
                        continue;
                    }
                    String groupId = instanceZKMetadata.getGroupId(resource);
                    String partitionId = instanceZKMetadata.getPartition(resource);
                    if (groupId != null && !groupId.isEmpty() && partitionId != null && !partitionId.isEmpty()) {
                        listOfSegmentsToAddToInstances.add(new Pair<String, String>(new HLCSegmentName(groupId, partitionId, String.valueOf(System.currentTimeMillis())).getSegmentName(), instanceId));
                    } else {
                        LOGGER.warn("Instance {} has invalid groupId ({}) and/or partitionId ({}) for resource {}, ignoring for segment assignment.", instanceId, groupId, partitionId, resource);
                        _controllerMetrics.addMeteredGlobalValue(ControllerMeter.CONTROLLER_REALTIME_TABLE_SEGMENT_ASSIGNMENT_ERROR, 1L);
                    }
                }
            } else {
                // Add all server instances to the list of instances for which to assign a realtime segment
                Set<String> instancesToAssignRealtimeSegment = new HashSet<String>();
                try {
                    instancesToAssignRealtimeSegment.addAll(_pinotHelixResourceManager.getServerInstancesForTable(resource, TableType.REALTIME));
                } catch (Exception e) {
                    LOGGER.error("Caught exception while fetching instances for resource {}", resource, e);
                    _controllerMetrics.addMeteredGlobalValue(ControllerMeter.CONTROLLER_REALTIME_TABLE_SEGMENT_ASSIGNMENT_ERROR, 1L);
                }
                // Remove server instances that are currently processing a segment
                for (String partition : state.getPartitionSet()) {
                    // Helix partition is the segment name
                    if (SegmentName.isHighLevelConsumerSegmentName(partition)) {
                        HLCSegmentName segName = new HLCSegmentName(partition);
                        RealtimeSegmentZKMetadata realtimeSegmentZKMetadata = ZKMetadataProvider.getRealtimeSegmentZKMetadata(_pinotHelixResourceManager.getPropertyStore(), segName.getTableName(), partition);
                        if (realtimeSegmentZKMetadata == null) {
                            // Segment was deleted by retention manager.
                            continue;
                        }
                        if (realtimeSegmentZKMetadata.getStatus() == Status.IN_PROGRESS) {
                            instancesToAssignRealtimeSegment.removeAll(state.getInstanceSet(partition));
                        }
                    }
                }
                // Assign a new segment to the server instances not currently processing this segment
                for (String instanceId : instancesToAssignRealtimeSegment) {
                    InstanceZKMetadata instanceZKMetadata = _pinotHelixResourceManager.getInstanceZKMetadata(instanceId);
                    String groupId = instanceZKMetadata.getGroupId(resource);
                    String partitionId = instanceZKMetadata.getPartition(resource);
                    listOfSegmentsToAddToInstances.add(new Pair<String, String>(new HLCSegmentName(groupId, partitionId, String.valueOf(System.currentTimeMillis())).getSegmentName(), instanceId));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Caught exception while processing resource {}, skipping.", resource, e);
            _controllerMetrics.addMeteredGlobalValue(ControllerMeter.CONTROLLER_REALTIME_TABLE_SEGMENT_ASSIGNMENT_ERROR, 1L);
        }
    }
    LOGGER.info("Computed list of new segments to add : " + Arrays.toString(listOfSegmentsToAddToInstances.toArray()));
    // Add the new segments to the server instances
    for (final Pair<String, String> segmentIdAndInstanceId : listOfSegmentsToAddToInstances) {
        final String segmentId = segmentIdAndInstanceId.getFirst();
        final String instanceName = segmentIdAndInstanceId.getSecond();
        try {
            final HLCSegmentName segName = new HLCSegmentName(segmentId);
            String resourceName = segName.getTableName();
            // Does the ideal state already contain this segment?
            if (!idealStateMap.get(resourceName).getPartitionSet().contains(segmentId)) {
                // No, add it
                // Create the realtime segment metadata
                RealtimeSegmentZKMetadata realtimeSegmentMetadataToAdd = new RealtimeSegmentZKMetadata();
                realtimeSegmentMetadataToAdd.setTableName(TableNameBuilder.extractRawTableName(resourceName));
                realtimeSegmentMetadataToAdd.setSegmentType(SegmentType.REALTIME);
                realtimeSegmentMetadataToAdd.setStatus(Status.IN_PROGRESS);
                realtimeSegmentMetadataToAdd.setSegmentName(segmentId);
                // Add the new metadata to the property store
                ZKMetadataProvider.setRealtimeSegmentZKMetadata(_pinotHelixResourceManager.getPropertyStore(), realtimeSegmentMetadataToAdd);
                // Update the ideal state to add the new realtime segment
                HelixHelper.updateIdealState(_pinotHelixResourceManager.getHelixZkManager(), resourceName, new Function<IdealState, IdealState>() {

                    @Override
                    public IdealState apply(IdealState idealState) {
                        return PinotTableIdealStateBuilder.addNewRealtimeSegmentToIdealState(segmentId, idealState, instanceName);
                    }
                }, RetryPolicies.exponentialBackoffRetryPolicy(5, 500L, 2.0f));
            }
        } catch (Exception e) {
            LOGGER.warn("Caught exception while processing segment {} for instance {}, skipping.", segmentId, instanceName, e);
            _controllerMetrics.addMeteredGlobalValue(ControllerMeter.CONTROLLER_REALTIME_TABLE_SEGMENT_ASSIGNMENT_ERROR, 1L);
        }
    }
}
Also used : HLCSegmentName(com.linkedin.pinot.common.utils.HLCSegmentName) KafkaStreamMetadata(com.linkedin.pinot.common.metadata.stream.KafkaStreamMetadata) HashMap(java.util.HashMap) InstanceZKMetadata(com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) JSONException(org.json.JSONException) IOException(java.io.IOException) RealtimeSegmentZKMetadata(com.linkedin.pinot.common.metadata.segment.RealtimeSegmentZKMetadata) AbstractTableConfig(com.linkedin.pinot.common.config.AbstractTableConfig) Pair(com.linkedin.pinot.core.query.utils.Pair) HashSet(java.util.HashSet)

Example 3 with InstanceZKMetadata

use of com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata in project pinot by linkedin.

the class LLRealtimeSegmentDataManagerTest method testTimeString.

@Test
public void testTimeString() throws Exception {
    JSONObject tableConfigJson = new JSONObject(_tableConfigJson);
    JSONObject tableIndexConfig = (JSONObject) tableConfigJson.get("tableIndexConfig");
    JSONObject streamConfigs = (JSONObject) tableIndexConfig.get("streamConfigs");
    {
        streamConfigs.put(CommonConstants.Helix.DataSource.Realtime.REALTIME_SEGMENT_FLUSH_TIME, "3h");
        AbstractTableConfig tableConfig = createTableConfig(tableConfigJson.toString());
        InstanceZKMetadata instanceZKMetadata = new InstanceZKMetadata();
        Schema schema = Schema.fromString(makeSchema());
        KafkaLowLevelStreamProviderConfig config = new KafkaLowLevelStreamProviderConfig();
        config.init(tableConfig, instanceZKMetadata, schema);
        Assert.assertEquals(3 * 3600 * 1000L, config.getTimeThresholdToFlushSegment());
    }
    {
        streamConfigs.put(CommonConstants.Helix.DataSource.Realtime.REALTIME_SEGMENT_FLUSH_TIME, "3h30m");
        AbstractTableConfig tableConfig = createTableConfig(tableConfigJson.toString());
        InstanceZKMetadata instanceZKMetadata = new InstanceZKMetadata();
        Schema schema = Schema.fromString(makeSchema());
        KafkaLowLevelStreamProviderConfig config = new KafkaLowLevelStreamProviderConfig();
        config.init(tableConfig, instanceZKMetadata, schema);
        Assert.assertEquals((3 * 3600 + 30 * 60) * 1000L, config.getTimeThresholdToFlushSegment());
    }
    {
        final long segTime = 898789748357L;
        streamConfigs.put(CommonConstants.Helix.DataSource.Realtime.REALTIME_SEGMENT_FLUSH_TIME, String.valueOf(segTime));
        AbstractTableConfig tableConfig = createTableConfig(tableConfigJson.toString());
        InstanceZKMetadata instanceZKMetadata = new InstanceZKMetadata();
        Schema schema = Schema.fromString(makeSchema());
        KafkaLowLevelStreamProviderConfig config = new KafkaLowLevelStreamProviderConfig();
        config.init(tableConfig, instanceZKMetadata, schema);
        Assert.assertEquals(segTime, config.getTimeThresholdToFlushSegment());
    }
}
Also used : JSONObject(org.json.JSONObject) InstanceZKMetadata(com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata) Schema(com.linkedin.pinot.common.data.Schema) KafkaLowLevelStreamProviderConfig(com.linkedin.pinot.core.realtime.impl.kafka.KafkaLowLevelStreamProviderConfig) AbstractTableConfig(com.linkedin.pinot.common.config.AbstractTableConfig) Test(org.testng.annotations.Test)

Example 4 with InstanceZKMetadata

use of com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata in project pinot by linkedin.

the class InstanceZKMetadataTest method instanceZKMetadataConvertionTest.

@Test
public void instanceZKMetadataConvertionTest() {
    ZNRecord znRecord = getTestInstanceZNRecord();
    InstanceZKMetadata instanceMetadataFromZNRecord = new InstanceZKMetadata(znRecord);
    InstanceZKMetadata instanceMetadata = getTestInstanceMetadata();
    ZNRecord znRecordFromMetadata = instanceMetadata.toZNRecord();
    Assert.assertTrue(MetadataUtils.comparisonZNRecords(znRecord, znRecordFromMetadata));
    Assert.assertTrue(instanceMetadata.equals(instanceMetadataFromZNRecord));
    Assert.assertTrue(instanceMetadata.equals(new InstanceZKMetadata(instanceMetadata.toZNRecord())));
    Assert.assertTrue(MetadataUtils.comparisonZNRecords(znRecord, new InstanceZKMetadata(znRecord).toZNRecord()));
}
Also used : InstanceZKMetadata(com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 5 with InstanceZKMetadata

use of com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata in project pinot by linkedin.

the class PinotHelixResourceManager method deleteRealtimeTable.

public void deleteRealtimeTable(String tableName) {
    final String realtimeTableName = TableNameBuilder.REALTIME_TABLE_NAME_BUILDER.forTable(tableName);
    // Remove from brokerResource
    HelixHelper.removeResourceFromBrokerIdealState(_helixZkManager, realtimeTableName);
    // Delete data table
    if (!_helixAdmin.getResourcesInCluster(_helixClusterName).contains(realtimeTableName)) {
        return;
    }
    // remove from property store
    ZKMetadataProvider.removeResourceSegmentsFromPropertyStore(getPropertyStore(), realtimeTableName);
    ZKMetadataProvider.removeResourceConfigFromPropertyStore(getPropertyStore(), realtimeTableName);
    ZKMetadataProvider.removeKafkaPartitionAssignmentFromPropertyStore(getPropertyStore(), realtimeTableName);
    // Remove groupId/PartitionId mapping for realtime table type.
    for (String instance : getAllInstancesForTable(realtimeTableName)) {
        InstanceZKMetadata instanceZKMetadata = ZKMetadataProvider.getInstanceZKMetadata(getPropertyStore(), instance);
        if (instanceZKMetadata != null) {
            instanceZKMetadata.removeResource(realtimeTableName);
            ZKMetadataProvider.setInstanceZKMetadata(getPropertyStore(), instanceZKMetadata);
        }
    }
    // dropping table
    _helixAdmin.dropResource(_helixClusterName, realtimeTableName);
}
Also used : InstanceZKMetadata(com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata)

Aggregations

InstanceZKMetadata (com.linkedin.pinot.common.metadata.instance.InstanceZKMetadata)8 AbstractTableConfig (com.linkedin.pinot.common.config.AbstractTableConfig)3 Schema (com.linkedin.pinot.common.data.Schema)2 HashMap (java.util.HashMap)2 ZNRecord (org.apache.helix.ZNRecord)2 Test (org.testng.annotations.Test)2 LLCRealtimeSegmentZKMetadata (com.linkedin.pinot.common.metadata.segment.LLCRealtimeSegmentZKMetadata)1 RealtimeSegmentZKMetadata (com.linkedin.pinot.common.metadata.segment.RealtimeSegmentZKMetadata)1 KafkaStreamMetadata (com.linkedin.pinot.common.metadata.stream.KafkaStreamMetadata)1 ServerMetrics (com.linkedin.pinot.common.metrics.ServerMetrics)1 HLCSegmentName (com.linkedin.pinot.common.utils.HLCSegmentName)1 Pair (com.linkedin.pinot.core.query.utils.Pair)1 KafkaLowLevelStreamProviderConfig (com.linkedin.pinot.core.realtime.impl.kafka.KafkaLowLevelStreamProviderConfig)1 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IdealState (org.apache.helix.model.IdealState)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1