You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2020/07/29 07:31:28 UTC

[GitHub] [cassandra-sidecar] dineshjoshi commented on a change in pull request #14: Jon/23/multiple cassandra versions

dineshjoshi commented on a change in pull request #14:
URL: https://github.com/apache/cassandra-sidecar/pull/14#discussion_r461205490



##########
File path: common/src/test/java/org/apache/cassandra/sidecar/common/CassandraVersionTest.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.common;
+
+import org.junit.jupiter.api.Test;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+/**
+ * Tests that CassandraVersion comparisons work correctly
+ */
+public class CassandraVersionTest

Review comment:
       Need to add negative test cases for `CassandraVersion::create(String v)`. You should also add tests to validate that the result of `CassandraVersion::toString` is parseable by `CassandraVersion::create(String v)`. 

##########
File path: common/src/main/java/org/apache/cassandra/sidecar/common/CassandraAdapterDelegate.java
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.common;
+
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.base.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Host;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.exceptions.NoHostAvailableException;
+
+
+/**
+ * Since it's possible for the version of Cassandra to change under us, we need this delegate to wrap the functionality
+ * of the underlying Cassandra adapter.  If a server reboots, we can swap out the right Adapter when the driver
+ * reconnects.
+ *
+ * This delegate *MUST* checkSession() before every call, because:
+ *
+ * 1. The session lazily connects
+ * 2. We might need to swap out the adapter if the version has changed
+ *
+ */
+public class CassandraAdapterDelegate implements ICassandraAdapter, Host.StateListener
+{
+    private final CQLSession cqlSession;
+    private final CassandraVersionProvider versionProvider;
+    private Session session;
+    private CassandraVersion currentVersion;
+    private ICassandraAdapter adapter;
+    private Boolean isUp = false;
+    private final int refreshRate;
+
+    private static final Logger logger = LoggerFactory.getLogger(CassandraAdapterDelegate.class);
+    private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
+    private boolean registered = false;
+
+    public CassandraAdapterDelegate(CassandraVersionProvider provider, CQLSession cqlSession)
+    {
+        this(provider, cqlSession, 5000);
+    }
+
+    public CassandraAdapterDelegate(CassandraVersionProvider provider, CQLSession cqlSession, int refreshRate)
+    {
+        this.cqlSession = cqlSession;
+        this.versionProvider = provider;
+        this.refreshRate = refreshRate;
+    }
+
+    public synchronized void start()
+    {
+        logger.info("Starting health check");
+        // TODO: maybe unhardcode

Review comment:
       Do you want to address this before merging?

##########
File path: cassandra-integration-tests/src/test/java/org/apache/cassandra/sidecar/common/testing/CassandraPod.java
##########
@@ -0,0 +1,360 @@
+/*
+ * 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.common.testing;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.kubernetes.client.Exec;
+
+import io.kubernetes.client.openapi.ApiClient;
+import io.kubernetes.client.openapi.ApiException;
+import io.kubernetes.client.openapi.Configuration;
+
+import io.kubernetes.client.openapi.apis.CoreV1Api;
+import io.kubernetes.client.openapi.models.V1ContainerPort;
+import io.kubernetes.client.openapi.models.V1Pod;
+import io.kubernetes.client.openapi.models.V1PodBuilder;
+import io.kubernetes.client.openapi.models.V1Service;
+import io.kubernetes.client.openapi.models.V1ServiceBuilder;
+import io.kubernetes.client.openapi.models.V1ServicePort;
+import io.kubernetes.client.openapi.models.V1ServiceSpec;
+
+import io.kubernetes.client.util.ClientBuilder;
+import okhttp3.OkHttpClient;
+import okhttp3.Protocol;
+import okhttp3.internal.Util;
+
+/**
+ * Manages a single instance of a Cassandra container
+ */
+class CassandraPod
+{
+    private final URI dockerRegistry;
+    private final String image;
+    private final String namespace;
+    private final String dockerGroup;
+    private static final Logger logger = LoggerFactory.getLogger(CassandraPod.class);
+    private final String podName;
+
+    private final CoreV1Api coreV1Api;
+    private Boolean deleted = false;
+
+    String ip;
+    Integer port;
+
+    public CassandraPod(URI dockerRegistry, String dockerGroup, String image, String namespace, CoreV1Api coreV1Api)
+    {
+        this.dockerRegistry = dockerRegistry;
+        this.image = image;
+        this.namespace = namespace;
+        this.dockerGroup = dockerGroup;
+        this.podName = String.format("cassandra-%s", UUID.randomUUID());
+        this.coreV1Api = coreV1Api;
+    }
+
+    /**
+     * Creates a single pod using the system properties passed in through Gradle
+     * @param image
+     * @return
+     * @throws Exception
+     */
+    public static CassandraPod createFromProperties(String image) throws Exception
+    {
+        URI dockerRegistry = new URI(System.getProperty("sidecar.dockerRegistry"));
+        String namespace = System.getProperty("sidecar.kubernetesNamespace");
+        String dockerGroup = System.getProperty("sidecar.dockerGroup");
+
+        logger.info("Creating pod from registry {}, namespace {}, group {}", dockerRegistry, namespace, dockerGroup);
+
+        if (dockerRegistry == null)
+        {
+            throw new Exception("Docker registry required but sidecar.dockerRegistry = null");
+        }
+        if (namespace == null)
+        {
+            throw new Exception("sidecar.kubernetesNamespace is not defined and is required for K8 testing");
+        }
+
+        ApiClient apiClient = ClientBuilder.standard().build();
+
+        // this is a workaround for socket errors that show up in certain JVM versions...
+        // without it, the tests fail in CI.
+        // we can probably get rid of this when we either move to JDK 11 only or if the Kubernetes clienti s updated
+        OkHttpClient httpClient =
+                apiClient.getHttpClient().newBuilder()
+                        .protocols(Util.immutableList(Protocol.HTTP_1_1))
+                        .readTimeout(10, TimeUnit.SECONDS)
+                        .writeTimeout(10, TimeUnit.SECONDS)
+                        .connectTimeout(10, TimeUnit.SECONDS)
+                        .callTimeout(10, TimeUnit.SECONDS)
+                        .retryOnConnectionFailure(true)
+                        .build();
+        apiClient.setHttpClient(httpClient);
+
+        Configuration.setDefaultApiClient(apiClient);
+
+        logger.info("K8 client: {}", apiClient.getBasePath());
+
+        CoreV1Api coreV1Api = new CoreV1Api(apiClient);
+
+
+        return new CassandraPod(dockerRegistry, dockerGroup, image, namespace, coreV1Api);
+    }
+
+    public void start() throws ApiException, InterruptedException, CassandraPodException
+    {
+        // create a v1 deployment spec
+        String fullImage = getFullImageName();
+
+        // similar to the spec yaml file, just programmatic
+
+        HashMap<String, String> labels = getLabels();
+
+        V1Service serviceBuilder = getService();
+
+        logger.debug("Exposing service with: {}", serviceBuilder.toString());
+
+        try
+        {
+            coreV1Api.createNamespacedService(namespace, serviceBuilder, null, null, null);
+        }
+        catch (ApiException e)
+        {
+            logger.error("Unable to create namespaced service: {}", e.getMessage());
+            throw e;
+        }
+
+        // get the service
+        V1Service namespacedService = coreV1Api.readNamespacedService(podName, namespace, null, null, null);
+        logger.debug("Service result: {}", namespacedService);
+
+        V1ServiceSpec serviceSpec = namespacedService.getSpec();
+
+        logger.info("Starting container {}", fullImage);
+        V1Pod pod = getPod(fullImage, labels);
+
+        logger.debug("Pod spec: {}", pod);
+        V1Pod podResult = coreV1Api.createNamespacedPod(namespace, pod, null, null, null);
+        logger.debug("Pod result: {}", podResult);
+
+        int maxTime = 30;
+        V1Pod namespacedPod = null;
+        Boolean started = false;
+        String response = "";
+
+        for (int i = 0; i < maxTime; i++)
+        {
+            // we sleep in the beginning because the pod will never be ready right away
+            // sometimes K8 seems to hang in CI as well, so this might be enough to let the pod start
+            try
+            {
+                Thread.sleep(1000);

Review comment:
       Potentially log a debug statement right before sleeping?

##########
File path: common/src/main/java/org/apache/cassandra/sidecar/common/CassandraVersion.java
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.common;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.base.Objects;
+
+/**
+ * Implements versioning used in Cassandra and CQL.
+ * <p>
+ * Note: The following code uses a slight variation from the semver document (http://semver.org).
+ * </p>
+ *
+ * The rules here are a bit different than normal semver comparison.  For simplicity,
+ * an alpha version of 4.0 or a snapshot is equal to 4.0.  This allows us to test sidecar
+ * against alpha versions of a release.
+ */
+public class CassandraVersion implements Comparable<CassandraVersion>
+{
+    /**
+     * note: 3rd group matches to words but only allows number and checked after regexp test.
+     * this is because 3rd and the last can be identical.
+     **/
+    private static final String VERSION_REGEXP = "(\\d+)\\.(\\d+)(?:\\.(\\w+))?(\\-[.\\w]+)?([.+][.\\w]+)?";
+
+    private static final Pattern pattern = Pattern.compile(VERSION_REGEXP);
+    private static final Pattern SNAPSHOT = Pattern.compile("-SNAPSHOT");
+
+    public final int major;
+    public final int minor;
+    public final int patch;
+
+    // not going to use these for equality checks but we might need to know what it is at some point
+    private final String extra;

Review comment:
       What is extra? I presume strings like alpha, beta, etc? If so 4.0.0-alpha4 and 4.0.0-alpha5 are technically not the same releases.

##########
File path: cassandra-integration-tests/src/test/java/org/apache/cassandra/sidecar/common/StatusTest.java
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.common;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.Session;
+import org.apache.cassandra.sidecar.common.testing.CassandraIntegrationTest;
+import org.apache.cassandra.sidecar.common.testing.CassandraTestContext;
+
+/**
+ * Placeholder test
+ */
+public class StatusTest
+{
+    private static final Logger logger = LoggerFactory.getLogger(StatusTest.class);
+
+    @BeforeEach
+    void setupData(CassandraTestContext context)
+    {
+        logger.info("setup {}", context.container);
+

Review comment:
       Nit: Extra newline.

##########
File path: scripts/check-kube.sh
##########
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash

Review comment:
       Empty script?




----------------------------------------------------------------
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



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