cassandra-pr mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [cassandra-sidecar] dineshjoshi commented on a change in pull request #4: Virtual tables API endpoints for sidecar for CASSANDRA-15124
Date Thu, 22 Aug 2019 18:29:42 GMT
dineshjoshi commented on a change in pull request #4: Virtual tables API endpoints for sidecar
for CASSANDRA-15124
URL: https://github.com/apache/cassandra-sidecar/pull/4#discussion_r316817455
 
 

 ##########
 File path: src/main/java/org/apache/cassandra/sidecar/VirtualTables.java
 ##########
 @@ -0,0 +1,342 @@
+/*
+ * 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 org.apache.cassandra.sidecar;
+
+import java.net.InetAddress;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.UUID;
+import javax.annotation.Nullable;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import org.apache.commons.lang3.NotImplementedException;
+
+import com.datastax.driver.core.Host;
+import com.datastax.driver.core.ResultSetFuture;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.exceptions.NoHostAvailableException;
+import com.datastax.driver.mapping.DefaultNamingStrategy;
+import com.datastax.driver.mapping.DefaultPropertyMapper;
+import com.datastax.driver.mapping.Mapper;
+import com.datastax.driver.mapping.MappingConfiguration;
+import com.datastax.driver.mapping.MappingManager;
+import com.datastax.driver.mapping.NamingConventions;
+import com.datastax.driver.mapping.PropertyAccessStrategy;
+import com.datastax.driver.mapping.Result;
+import com.datastax.driver.mapping.annotations.Table;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.vertx.core.json.JsonObject;
+
+/**
+ * Exposee virtual tables contents as pojos
+ */
+@Singleton
+public class VirtualTables
+{
+    private final CQLSession session;
+    private MappingConfiguration conf;
+
+    private MappingManager mappingManager;
+
+    @Inject
+    public VirtualTables(CQLSession session)
+    {
+        this.session = session;
+        conf = MappingConfiguration.builder()
+                            .withPropertyMapper(
+                                new DefaultPropertyMapper()
+                                    .setNamingStrategy(
+                                        new DefaultNamingStrategy(
+                                            NamingConventions.LOWER_CAMEL_CASE, NamingConventions.LOWER_SNAKE_CASE))
+                                    .setPropertyAccessStrategy(PropertyAccessStrategy.FIELDS))
+                            .build();
+    }
+
+    @Nullable
+    MappingManager manger()
+    {
+        if (mappingManager != null)
+            return mappingManager;
+        if (session.getLocalCql() == null)
+            return null;
+
+        // its not ideal but ok if we create multiple during race
+        mappingManager = new MappingManager(session.getLocalCql(), conf);
+        return mappingManager;
+    }
+
+    public <T> ListenableFuture<Result<T>> getTableResults(Class<T>
pojo)
+    {
+        Table[] annotation = pojo.getAnnotationsByType(Table.class);
+        if (annotation.length != 1 || !annotation[0].keyspace().equals("system_views"))
+            throw new IllegalArgumentException("Invalid class type");
+
+        if (manger() == null)
+            throw new NoHostAvailableException(Collections.emptyMap());
+
+        Session local = session.getLocalCql();
+        assert local != null; // manager() null check should guard against this
+
+        Collection<Host> hosts = local.getState().getConnectedHosts();
+        if (hosts.isEmpty())
+            throw new NoHostAvailableException(Collections.emptyMap());
+        if (hosts.iterator().next().getCassandraVersion().getMajor() < 4)
+            throw new NotImplementedException("Requires version 4.0+");
+
+        String table = annotation[0].name();
+        Mapper<T> mapper = mappingManager.mapper(pojo);
+
+        ResultSetFuture results = local.executeAsync("SELECT * FROM system_views." + table);
+        return mapper.mapAsync(results);
+    }
+
+    /**
+     * Represents state of a thread pool from the thread_pools virtual table
+     */
+    @Table(keyspace = "system_views", name = "thread_pools")
+    public static class ThreadPool
+    {
+        @JsonIgnore
+        private String name;
+        private int activeTasks;
+        private int activeTasksLimit;
+        private int pendingTasks;
+        private long completedTasks;
+        private long blockedTasks;
+        private long blockedTasksAllTime;
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public int getActiveTasks()
+        {
+            return activeTasks;
+        }
+
+        public int getActiveTasksLimit()
+        {
+            return activeTasksLimit;
+        }
+
+        public int getPendingTasks()
+        {
+            return pendingTasks;
+        }
+
+        public long getCompletedTasks()
+        {
+            return completedTasks;
+        }
+
+        public long getBlockedTasks()
+        {
+            return blockedTasks;
+        }
+
+        public long getBlockedTasksAllTime()
+        {
+            return blockedTasksAllTime;
+        }
+
+        public JsonObject json()
 
 Review comment:
   Do we need to customize this?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Mime
View raw message