ChinmaySKulkarni commented on a change in pull request #696: PHOENIX-5607 
Client-server backward compatibility tests
URL: https://github.com/apache/phoenix/pull/696#discussion_r383559202
 
 

 ##########
 File path: 
phoenix-core/src/it/java/org/apache/phoenix/end2end/BackwardCompatibilityIT.java
 ##########
 @@ -0,0 +1,353 @@
+/*
+ * 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.phoenix.end2end;
+
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeFalse;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.InputStreamReader;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.util.Collection;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.curator.shaded.com.google.common.collect.Lists;
+import org.apache.phoenix.coprocessor.MetaDataProtocol;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.apache.phoenix.util.ReadOnlyProps;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * This class is meant for testing all compatible client versions 
+ * against the current server version. It runs SQL queries with given 
+ * client versions and compares the output against gold files
+ */
+
+@RunWith(Parameterized.class)
+public class BackwardCompatibilityIT extends BaseOwnClusterIT {
+
+    private static final String SQL_DIR = "src/it/resources/sql_files/";
+    private static final String RESULT_DIR = "src/it/resources/gold_files/";
+    private static final String RESULT_PREFIX = "result_";
+    private static final String SQL_EXTENSION = ".sql";
+    private static final String TEXT_EXTENSION = ".txt";
+    private static final String CREATE_ADD = "create_add";
+    private static final String ADD_DATA = "add_data";
+    private static final String ADD_DELETE = "add_delete";
+    private static final String QUERY = "query";
+    private static final String QUERY_ADD_DELETE = "query_add_delete";
+
+    private final String compatibleClientVersion;
+
+    public BackwardCompatibilityIT(String compatibleClientVersion) {
+        this.compatibleClientVersion = compatibleClientVersion;
+    }
+
+    @Parameters(name = "BackwardCompatibilityIT_compatibleClientVersion={0}")
+    public static synchronized Collection<String> data() {
+        return MetaDataProtocol.COMPATIBLE_CLIENT_VERSIONS;
+    }
+
+    @Before
+    public synchronized void doSetup() throws Exception {
+        setUpTestDriver(ReadOnlyProps.EMPTY_PROPS);
+    }
+
+    /**
+     * Scenario: 
+     * 1. Old Client connects to the updated server 
+     * 2. Old Client creates tables and inserts data 
+     * 3. New Client reads the data inserted by the old client
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testUpsertWithOldClient() throws Exception {
+        checkForCurrentVersion();
+        // Insert data with old client and read with new client
+        executeQueryWithClientVersion(compatibleClientVersion, CREATE_ADD);
+        executeQueriesWithCurrentVersion(QUERY);
+        assertEquals(true, compareOutput(CREATE_ADD, QUERY));
+    }
+
+    /**
+     * Scenario: 
+     * 1. New Client connects to the updated server 
+     * 2. New Client creates tables and inserts data 
+     * 3. Old Client reads the data inserted by the old client
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testSelectWithOldClient() throws Exception {
+        checkForCurrentVersion();
+        // Insert data with new client and read with old client
+        executeQueriesWithCurrentVersion(CREATE_ADD);
+        executeQueryWithClientVersion(compatibleClientVersion, QUERY);
+        assertEquals(true, compareOutput(CREATE_ADD, QUERY));
+    }
+
+    /**
+     * Scenario: 
+     * 1. Old Client connects to the updated server 
+     * 2. Old Client creates tables and inserts data 
+     * 3. New Client reads the data inserted by the old client 
+     * 4. New Client inserts more data into the tables created by old client 
+     * 5. Old Client reads the data inserted by new client
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testSelectUpsertWithNewClient() throws Exception {
+        checkForCurrentVersion();
+        // Insert data with old client and read with new client
+        executeQueryWithClientVersion(compatibleClientVersion, CREATE_ADD);
+        executeQueriesWithCurrentVersion(QUERY);
+        assertEquals(true, compareOutput(CREATE_ADD, QUERY));
+
+        // Insert more data with new client and read with old client
+        executeQueriesWithCurrentVersion(ADD_DATA);
+        executeQueryWithClientVersion(compatibleClientVersion, QUERY);
+        assertEquals(true, compareOutput(ADD_DATA, QUERY));
+    }
+
+    /**
+     * Scenario: 
+     * 1. New Client connects to the updated server 
+     * 2. New Client creates tables and inserts data 
+     * 3. Old Client reads the data inserted by the old client 
+     * 4. Old Client inserts more data into the tables created by old client 
+     * 5. New Client reads the data inserted by new client
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testSelectUpsertWithOldClient() throws Exception {
+        checkForCurrentVersion();
+        // Insert data with new client and read with old client
+        executeQueriesWithCurrentVersion(CREATE_ADD);
+        executeQueryWithClientVersion(compatibleClientVersion, QUERY);
+        assertEquals(true, compareOutput(CREATE_ADD, QUERY));
+
+        // Insert more data with old client and read with new client
+        executeQueryWithClientVersion(compatibleClientVersion, ADD_DATA);
+        executeQueriesWithCurrentVersion(QUERY);
+        assertEquals(true, compareOutput(ADD_DATA, QUERY));
+    }
+
+    /**
+     * Scenario: 
+     * 1. Old Client connects to the updated server 
+     * 2. Old Client creates tables and inserts data 
+     * 3. New Client reads the data inserted by the old client 
+     * 4. Old Client creates and deletes the data
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testUpsertDeleteWithOldClient() throws Exception {
+        checkForCurrentVersion();
+        // Insert data with old client and read with new client
+        executeQueryWithClientVersion(compatibleClientVersion, CREATE_ADD);
+        executeQueriesWithCurrentVersion(QUERY);
+        assertEquals(true, compareOutput(CREATE_ADD, QUERY));
+
+        // Deletes with the old client
+        executeQueryWithClientVersion(compatibleClientVersion, ADD_DELETE);
+        executeQueryWithClientVersion(compatibleClientVersion, 
QUERY_ADD_DELETE);
+        assertEquals(true, compareOutput(ADD_DELETE, QUERY_ADD_DELETE));
+    }
+
+    /**
+     * Scenario: 
+     * 1. New Client connects to the updated server 
+     * 2. New Client creates tables and inserts data 
+     * 3. Old Client reads the data inserted by the old client 
+     * 4. New Client creates and deletes the data
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testUpsertDeleteWithNewClient() throws Exception {
+        checkForCurrentVersion();
+        // Insert data with old client and read with new client
+        executeQueriesWithCurrentVersion(CREATE_ADD);
+        executeQueryWithClientVersion(compatibleClientVersion, QUERY);
+        assertEquals(true, compareOutput(CREATE_ADD, QUERY));
+
+        // Deletes with the new client
+        executeQueriesWithCurrentVersion(ADD_DELETE);
+        executeQueriesWithCurrentVersion(QUERY_ADD_DELETE);
+        assertEquals(true, compareOutput(ADD_DELETE, QUERY_ADD_DELETE));
+    }
+    
+    private void checkForCurrentVersion() {
+        // For the first code cut of any major version, there wouldn't be any 
backward compatible
+        // clients. Hence the test wouldn't run and just return true when the 
client  
+        // version to be tested is same as current version
+        
assumeFalse(compatibleClientVersion.contains(MetaDataProtocol.CURRENT_CLIENT_VERSION));
+    }
+
+    // Executes the queries listed in the operation file with a given client 
version
+    private void executeQueryWithClientVersion(String clientVersion, String 
operation)
+            throws Exception {
+        String BASH = "/bin/bash";
+        String EXECUTE_QUERY_SH = "src/it/scripts/execute_query.sh";
+
+        List<String> cmdParams = Lists.newArrayList();
+        cmdParams.add(BASH);
+        cmdParams.add(EXECUTE_QUERY_SH);
+        cmdParams.add(getZkUrl());
+        cmdParams.add(clientVersion);
+
+        cmdParams.add(new File(SQL_DIR + operation + 
SQL_EXTENSION).getAbsolutePath());
+        cmdParams.add(
+            new File(RESULT_DIR + RESULT_PREFIX + operation + 
TEXT_EXTENSION).getAbsolutePath());
+        cmdParams.add(System.getProperty("java.io.tmpdir"));
+
+        if (System.getProperty("maven.home") != null) {
+            cmdParams.add(System.getProperty("maven.home"));
+        }
+
+        ProcessBuilder pb = new ProcessBuilder(cmdParams);
+        final Process p = pb.start();
+        final StringBuffer sb = new StringBuffer();
+        //Capture the error stream if any from the execution of the script
+        Thread errorStreamThread = new Thread() {
+            @Override
+            public void run() {
+                try {
+                    BufferedReader reader = new BufferedReader(
+                            new InputStreamReader(p.getErrorStream()));
+                    String line = null;
+                    while ((line = reader.readLine()) != null) {
+                        sb.append(line);
+                    }
+                    reader.close();
+                } catch (final Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        };
+        errorStreamThread.start();
+        assertEquals(sb.toString(), 0, p.waitFor());
+    }
+
+    // Executes the queries listed in the given operation file from the 
sql_files directory
+    private ResultSet executeQueriesWithCurrentVersion(String operation) 
throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        StringBuilder sb = new StringBuilder();
+        BufferedReader reader =
+                new BufferedReader(new FileReader(SQL_DIR + operation + 
SQL_EXTENSION));
+        String query;
+        ResultSet rs = null;
+        while ((query = reader.readLine()) != null) {
+            query = query.trim();
+            if (query.length() == 0 || query.startsWith("/") || 
query.startsWith("*")) continue;
+            sb.append(query);
+        }
+        reader.close();
+        
+        String[] queries = sb.toString().split(";");
+        BufferedWriter br = new BufferedWriter(
+                        new FileWriter(RESULT_DIR + RESULT_PREFIX + operation 
+ TEXT_EXTENSION));
+        for (int i = 0; i < queries.length; i++) {
+            PreparedStatement stmt = conn.prepareStatement(queries[i]);
+            stmt.execute();
+            rs = stmt.getResultSet();
+            if (rs != null) {
+                saveResultSet(rs, br);
+            }
+            conn.commit();
 
 Review comment:
   Do we need this commit? We are only issuing queries right?

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

Reply via email to