jon-wei commented on a change in pull request #8547: Add `sys.supervisors` 
table to system tables
URL: https://github.com/apache/incubator-druid/pull/8547#discussion_r336292901
 
 

 ##########
 File path: 
sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemSchema.java
 ##########
 @@ -755,6 +770,170 @@ public void close()
     );
   }
 
+  /**
+   * This table contains a row per supervisor task.
+   */
+  static class SupervisorsTable extends AbstractTable implements ScannableTable
+  {
+    private final DruidLeaderClient druidLeaderClient;
+    private final ObjectMapper jsonMapper;
+    private final BytesAccumulatingResponseHandler responseHandler;
+    private final AuthorizerMapper authorizerMapper;
+
+    public SupervisorsTable(
+        DruidLeaderClient druidLeaderClient,
+        ObjectMapper jsonMapper,
+        BytesAccumulatingResponseHandler responseHandler,
+        AuthorizerMapper authorizerMapper
+    )
+    {
+      this.druidLeaderClient = druidLeaderClient;
+      this.jsonMapper = jsonMapper;
+      this.responseHandler = responseHandler;
+      this.authorizerMapper = authorizerMapper;
+    }
+
+
+    @Override
+    public RelDataType getRowType(RelDataTypeFactory typeFactory)
+    {
+      return SUPERVISOR_SIGNATURE.getRelDataType(typeFactory);
+    }
+
+    @Override
+    public TableType getJdbcTableType()
+    {
+      return TableType.SYSTEM_TABLE;
+    }
+
+    @Override
+    public Enumerable<Object[]> scan(DataContext root)
+    {
+      class SupervisorsEnumerable extends DefaultEnumerable<Object[]>
+      {
+        private final CloseableIterator<SupervisorStatus> it;
+
+        public SupervisorsEnumerable(JsonParserIterator<SupervisorStatus> 
tasks)
+        {
+          this.it = getAuthorizedSupervisors(tasks, root);
+        }
+
+        @Override
+        public Iterator<Object[]> iterator()
+        {
+          throw new UnsupportedOperationException("Do not use iterator(), it 
cannot be closed.");
+        }
+
+        @Override
+        public Enumerator<Object[]> enumerator()
+        {
+          return new Enumerator<Object[]>()
+          {
+            @Override
+            public Object[] current()
+            {
+              final SupervisorStatus supervisor = it.next();
+              return new Object[]{
+                  supervisor.getId(),
+                  supervisor.getState(),
+                  supervisor.getDetailedState(),
+                  supervisor.isHealthy() ? 1L : 0L,
+                  supervisor.getType(),
+                  supervisor.getSource(),
+                  supervisor.isSuspended() ? 1L : 0L,
+                  supervisor.getSpecString()
+              };
+            }
+
+            @Override
+            public boolean moveNext()
+            {
+              return it.hasNext();
+            }
+
+            @Override
+            public void reset()
+            {
+
+            }
+
+            @Override
+            public void close()
+            {
+              try {
+                it.close();
+              }
+              catch (IOException e) {
+                throw new RuntimeException(e);
+              }
+            }
+          };
+        }
+      }
+
+      return new SupervisorsEnumerable(getSupervisors(druidLeaderClient, 
jsonMapper, responseHandler));
+    }
+
+    private CloseableIterator<SupervisorStatus> getAuthorizedSupervisors(
+        JsonParserIterator<SupervisorStatus> it,
+        DataContext root
+    )
+    {
+      final AuthenticationResult authenticationResult =
+          (AuthenticationResult) 
root.get(PlannerContext.DATA_CTX_AUTHENTICATION_RESULT);
+
+      Function<SupervisorStatus, Iterable<ResourceAction>> raGenerator = 
supervisor -> Collections.singletonList(
+          
AuthorizationUtils.DATASOURCE_READ_RA_GENERATOR.apply(supervisor.getSource()));
+
+      final Iterable<SupervisorStatus> authorizedTasks = 
AuthorizationUtils.filterAuthorizedResources(
 
 Review comment:
   `authorizedTasks` -> `authorizedSupervisors`

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to