jon-wei commented on a change in pull request #6094: Introduce SystemSchema 
tables (#5989)
URL: https://github.com/apache/incubator-druid/pull/6094#discussion_r220019360
 
 

 ##########
 File path: sql/src/main/java/io/druid/sql/calcite/schema/SystemSchema.java
 ##########
 @@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package io.druid.sql.calcite.schema;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+import com.google.inject.Inject;
+import io.druid.client.DruidServer;
+import io.druid.client.ImmutableDruidDataSource;
+import io.druid.client.TimelineServerView;
+import io.druid.client.coordinator.Coordinator;
+import io.druid.client.indexing.IndexingService;
+import io.druid.client.selector.QueryableDruidServer;
+import io.druid.discovery.DruidLeaderClient;
+import io.druid.indexer.TaskStatusPlus;
+import io.druid.java.util.common.ISE;
+import io.druid.java.util.common.StringUtils;
+import io.druid.java.util.common.logger.Logger;
+import io.druid.java.util.http.client.response.FullResponseHolder;
+import io.druid.segment.column.ValueType;
+import io.druid.server.coordination.ServerType;
+import io.druid.server.security.AuthorizerMapper;
+import io.druid.sql.calcite.table.RowSignature;
+import io.druid.timeline.DataSegment;
+import org.apache.calcite.DataContext;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Linq4j;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.ScannableTable;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.calcite.schema.impl.AbstractTable;
+import org.jboss.netty.handler.codec.http.HttpMethod;
+import org.jboss.netty.handler.codec.http.HttpResponseStatus;
+import org.joda.time.DateTime;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class SystemSchema extends AbstractSchema
+{
+  private static final Logger log = new Logger(SystemSchema.class);
+
+  public static final String NAME = "sys";
+  private static final String SEGMENTS_TABLE = "segments";
+  private static final String SERVERS_TABLE = "servers";
+  private static final String SEGMENT_SERVERS_TABLE = "segment_servers";
+  private static final String TASKS_TABLE = "tasks";
+  private static final int SEGMENTS_TABLE_SIZE;
+  private static final int SEGMENT_SERVERS_TABLE_SIZE;
+
+  private static final RowSignature SEGMENTS_SIGNATURE = RowSignature
+      .builder()
+      .add("segment_id", ValueType.STRING)
+      .add("datasource", ValueType.STRING)
+      .add("start", ValueType.STRING)
+      .add("end", ValueType.STRING)
+      .add("size", ValueType.LONG)
+      .add("version", ValueType.STRING)
+      .add("partition_num", ValueType.STRING)
+      .add("num_replicas", ValueType.LONG)
+      .add("is_published", ValueType.LONG)
+      .add("is_available", ValueType.LONG)
+      .add("is_realtime", ValueType.LONG)
+      .add("payload", ValueType.STRING)
+      .build();
+
+  private static final RowSignature SERVERS_SIGNATURE = RowSignature
+      .builder()
+      .add("server", ValueType.STRING)
+      .add("scheme", ValueType.STRING)
+      .add("server_type", ValueType.STRING)
+      .add("tier", ValueType.STRING)
+      .add("curr_size", ValueType.LONG)
+      .add("max_size", ValueType.LONG)
+      .build();
+
+  private static final RowSignature SERVERSEGMENTS_SIGNATURE = RowSignature
+      .builder()
+      .add("server", ValueType.STRING)
+      .add("segment_id", ValueType.STRING)
+      .build();
+
+  private static final RowSignature TASKS_SIGNATURE = RowSignature
+      .builder()
+      .add("task_id", ValueType.STRING)
+      .add("type", ValueType.STRING)
+      .add("datasource", ValueType.STRING)
+      .add("created_time", ValueType.STRING)
+      .add("queue_insertion_time", ValueType.STRING)
+      .add("status", ValueType.STRING)
+      .add("runner_status", ValueType.STRING)
+      .add("duration", ValueType.STRING)
+      .add("location", ValueType.STRING)
+      .add("error_msg", ValueType.STRING)
+      .build();
+
+  private final Map<String, Table> tableMap;
+
+  static {
+    SEGMENTS_TABLE_SIZE = SEGMENTS_SIGNATURE.getRowOrder().size();
+    SEGMENT_SERVERS_TABLE_SIZE = SERVERSEGMENTS_SIGNATURE.getRowOrder().size();
+  }
+
+  @Inject
+  public SystemSchema(
+      final TimelineServerView serverView,
+      final AuthorizerMapper authorizerMapper,
+      final @Coordinator DruidLeaderClient coordinatorDruidLeaderClient,
+      final @IndexingService DruidLeaderClient overlordDruidLeaderClient,
+      final ObjectMapper jsonMapper
+  )
+  {
+    Preconditions.checkNotNull(serverView, "serverView");
+    this.tableMap = ImmutableMap.of(
+        SEGMENTS_TABLE, new SegmentsTable(serverView, 
coordinatorDruidLeaderClient, jsonMapper),
+        SERVERS_TABLE, new ServersTable(serverView),
+        SEGMENT_SERVERS_TABLE, new ServerSegmentsTable(serverView),
+        TASKS_TABLE, new TasksTable(overlordDruidLeaderClient, jsonMapper)
+    );
+  }
+
+  @Override
+  public Map<String, Table> getTableMap()
+  {
+    return tableMap;
+  }
+
+  static class SegmentsTable extends AbstractTable implements ScannableTable
+  {
+    private final TimelineServerView serverView;
+    private final DruidLeaderClient druidLeaderClient;
+    private final ObjectMapper jsonMapper;
+
+    public SegmentsTable(
+        TimelineServerView serverView,
+        DruidLeaderClient druidLeaderClient,
+        ObjectMapper jsonMapper
+    )
+    {
+      this.serverView = serverView;
+      this.druidLeaderClient = druidLeaderClient;
+      this.jsonMapper = jsonMapper;
+    }
+
+    @Override
+    public RelDataType getRowType(RelDataTypeFactory typeFactory)
+    {
+      return SEGMENTS_SIGNATURE.getRelDataType(typeFactory);
+    }
+
+    @Override
+    public TableType getJdbcTableType()
+    {
+      return TableType.SYSTEM_TABLE;
+    }
+
+    @Override
+    public Enumerable<Object[]> scan(DataContext root)
+    {
+      final List<Object[]> rows = new ArrayList<>();
+      final List<ImmutableDruidDataSource> druidDataSourceList = 
getMetadataSegments(druidLeaderClient, jsonMapper);
+      final List<DataSegment> metadataSegments = druidDataSourceList
+          .stream()
+          .flatMap(t -> t.getSegments().stream())
+          .collect(Collectors.toList());
+      final Map<String, DataSegment> publishedSegments = metadataSegments
+          .stream()
+          .collect(Collectors.toMap(
+              DataSegment::getIdentifier,
+              Function.identity()
+          ));
+      final Map<String, DataSegment> availableSegments = new HashMap<>();
+      final Map<String, QueryableDruidServer> serverViewClients = 
serverView.getClients();
+      for (QueryableDruidServer queryableDruidServer : 
serverViewClients.values()) {
+        final DruidServer druidServer = queryableDruidServer.getServer();
+        final ServerType type = druidServer.getType();
+        final Map<String, DataSegment> segments = new 
HashMap<>(druidServer.getSegments());
+        final long isRealtime = druidServer.segmentReplicatable() ? 0 : 1;
+        for (Map.Entry<String, DataSegment> segmentEntry : 
segments.entrySet()) {
+          String segmentId = segmentEntry.getKey();
+          DataSegment segment = segmentEntry.getValue();
+          int numReplicas = 1;
+          if (availableSegments.containsKey(segmentId)) {
+            //do not create new row if a segmentId has been seen previously
+            // but increment the replica count and update row
+            numReplicas++;
+            updateRow(segmentId, numReplicas, rows);
+            continue;
+          }
+          availableSegments.putIfAbsent(segmentId, segment);
+          long isAvailable = 0;
+          final long isPublished = publishedSegments.containsKey(segmentId) ? 
1 : 0;
+          if (type.toString().equals(ServerType.HISTORICAL.toString())
+              || type.toString().equals(ServerType.REALTIME.toString())
+              || 
type.toString().equals(ServerType.INDEXER_EXECUTOR.toString())) {
+            isAvailable = 1;
+          }
+          String payload;
+          try {
+            payload = jsonMapper.writeValueAsString(segment);
+          }
+          catch (JsonProcessingException e) {
+            log.error(e, "Error getting segment payload for segment %s", 
segmentId);
+            throw new RuntimeException(e);
+          }
+          final Object[] row = createRow(
+              segment.getIdentifier(),
+              segment.getDataSource(),
+              segment.getInterval().getStart(),
+              segment.getInterval().getEnd(),
+              segment.getSize(),
+              segment.getVersion(),
+              segment.getShardSpec().getPartitionNum(),
+              numReplicas,
+              isPublished,
+              isAvailable,
+              isRealtime,
+              payload
+          );
+          rows.add(row);
+        }
+      }
+      //process publishedSegments
+      for (Map.Entry<String, DataSegment> segmentEntry : 
publishedSegments.entrySet()) {
+        String segmentId = segmentEntry.getKey();
+        //skip the published segments which are already processed
+        if (availableSegments.containsKey(segmentId)) {
+          continue;
+        }
+        DataSegment segment = segmentEntry.getValue();
+        String payload;
+        try {
+          payload = jsonMapper.writeValueAsString(segment);
+        }
+        catch (JsonProcessingException e) {
+          log.error(e, "Error getting segment payload for segment %s", 
segmentId);
+          throw new RuntimeException(e);
+        }
+        final Object[] row = createRow(
+            segment.getIdentifier(),
+            segment.getDataSource(),
+            segment.getInterval().getStart(),
+            segment.getInterval().getEnd(),
+            segment.getSize(),
+            segment.getVersion(),
+            segment.getShardSpec().getPartitionNum(),
+            0,
+            1,
+            0,
+            0,
+            payload
+        );
+        rows.add(row);
+      }
+      return Linq4j.asEnumerable(rows);
+    }
+
+    private void updateRow(String segmentId, int replicas, List<Object[]> rows)
+    {
+      Object[] oldRow = null;
+      Object[] newRow = null;
+      for (Object[] row : rows) {
 
 Review comment:
   @surekhasaharan Was this comment addressed? 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to