Search in sources :

Example 1 with Compression

use of com.mysql.cj.conf.PropertyDefinitions.Compression in project aws-mysql-jdbc by awslabs.

the class CompressionTest method uplinkCompression.

/**
 * Tests uplink compression using each one of the compression options.
 */
@Test
public void uplinkCompression() {
    assumeTrue(this.compressionSettings.serverSupportsCompression(), "Server variable mysqlx_compression_algorithms must be configured to run this test.");
    dropCollection("uplinkCompression");
    for (Compression compr : Compression.values()) {
        this.schema.createCollection("uplinkCompression");
        // Replace DISABLED by default value.
        String testCase = "[Compression: " + (compr == Compression.DISABLED ? "<default>" : compr) + "]";
        Session testSession = this.fact.getSession(this.compressFreeBaseUrl + (compr == Compression.DISABLED ? "" : makeParam(PropertyKey.xdevapiCompression, compr)));
        Collection col = testSession.getDefaultSchema().getCollection("uplinkCompression");
        assertTrue(this.counters.resetCounters(), testCase);
        // Enough bytes to trigger compression.
        AddResult res = col.add(longData).execute();
        assertEquals(1, res.getAffectedItemsCount(), testCase);
        assertTrue(this.counters.resetCounters(), testCase);
        // Server compresses small messages anyway.
        assertTrue(this.counters.downlinkCompressionUsed(), testCase);
        assertTrue(this.counters.uplinkCompressionUsed(), testCase);
        testSession.close();
        testSession = this.fact.getSession(this.compressFreeBaseUrl + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED));
        col = testSession.getDefaultSchema().getCollection("uplinkCompression");
        DocResult docs = col.find().execute();
        assertEquals(1, docs.count(), testCase);
        assertEquals(longDataDoc.get("data").toString(), docs.fetchOne().get("data").toString(), testCase);
        assertTrue(this.counters.resetCounters(), testCase);
        assertFalse(this.counters.usedCompression(), testCase);
        dropCollection("uplinkCompression");
        testSession.close();
    }
}
Also used : Compression(com.mysql.cj.conf.PropertyDefinitions.Compression) Collection(com.mysql.cj.xdevapi.Collection) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 2 with Compression

use of com.mysql.cj.conf.PropertyDefinitions.Compression in project aws-mysql-jdbc by awslabs.

the class CompressionTest method downlinkCompression.

/**
 * Tests downlink compression using each one of the compression options.
 */
@Test
public void downlinkCompression() {
    assumeTrue(this.compressionSettings.serverSupportsCompression(), "Server variable mysqlx_compression_algorithms must be configured to run this test.");
    dropCollection("downlinkCompression");
    this.schema.createCollection("downlinkCompression");
    Session testSession = this.fact.getSession(this.compressFreeBaseUrl + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED));
    Collection col = testSession.getDefaultSchema().getCollection("downlinkCompression");
    assertTrue(this.counters.resetCounters());
    // Enough bytes to trigger compression.
    AddResult res = col.add(longData).execute();
    assertEquals(1, res.getAffectedItemsCount());
    assertTrue(this.counters.resetCounters());
    assertFalse(this.counters.usedCompression());
    for (Compression compr : Compression.values()) {
        testSession.close();
        // Replace DISABLED by default value.
        String testCase = "[Compression: " + (compr == Compression.DISABLED ? "<default>" : compr) + "]";
        testSession = this.fact.getSession(this.compressFreeBaseUrl + (compr == Compression.DISABLED ? "" : makeParam(PropertyKey.xdevapiCompression, compr)));
        col = testSession.getDefaultSchema().getCollection("downlinkCompression");
        DocResult docs = col.find().execute();
        assertEquals(1, docs.count(), testCase);
        assertEquals(longDataDoc.get("data").toString(), docs.fetchOne().get("data").toString(), testCase);
        assertTrue(this.counters.resetCounters(), testCase);
        assertFalse(this.counters.uplinkCompressionUsed(), testCase);
        assertTrue(this.counters.downlinkCompressionUsed(), testCase);
    }
    dropCollection("downlinkCompression");
    testSession.close();
}
Also used : Compression(com.mysql.cj.conf.PropertyDefinitions.Compression) Collection(com.mysql.cj.xdevapi.Collection) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 3 with Compression

use of com.mysql.cj.conf.PropertyDefinitions.Compression in project aws-mysql-jdbc by awslabs.

the class XProtocol method negotiateCompression.

/**
 * Negotiates compression capabilities with the server.
 */
public void negotiateCompression() {
    Compression compression = this.propertySet.<Compression>getEnumProperty(PropertyKey.xdevapiCompression.getKeyName()).getValue();
    if (compression == Compression.DISABLED) {
        return;
    }
    Map<String, List<String>> compressionCapabilities = this.serverSession.serverCapabilities.getCompression();
    if (compressionCapabilities.isEmpty() || !compressionCapabilities.containsKey(XServerCapabilities.SUBKEY_COMPRESSION_ALGORITHM) || compressionCapabilities.get(XServerCapabilities.SUBKEY_COMPRESSION_ALGORITHM).isEmpty()) {
        if (compression == Compression.REQUIRED) {
            throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("Protocol.Compression.0"));
        }
        // TODO Log "Compression negotiation failed. Connection will proceed uncompressed."
        return;
    }
    RuntimeProperty<String> compressionAlgorithmsProp = this.propertySet.getStringProperty(PropertyKey.xdevapiCompressionAlgorithms.getKeyName());
    String compressionAlgorithmsList = compressionAlgorithmsProp.getValue();
    compressionAlgorithmsList = compressionAlgorithmsList == null ? "" : compressionAlgorithmsList.trim();
    String[] compressionAlgorithmsOrder;
    String[] compressionAlgsOrder = compressionAlgorithmsList.split("\\s*,\\s*");
    compressionAlgorithmsOrder = Arrays.stream(compressionAlgsOrder).sequential().filter(n -> n != null && n.length() > 0).map(String::toLowerCase).map(CompressionAlgorithm::getNormalizedAlgorithmName).toArray(String[]::new);
    String compressionExtensions = this.propertySet.getStringProperty(PropertyKey.xdevapiCompressionExtensions.getKeyName()).getValue();
    compressionExtensions = compressionExtensions == null ? "" : compressionExtensions.trim();
    Map<String, CompressionAlgorithm> compressionAlgorithms = getCompressionExtensions(compressionExtensions);
    Optional<String> algorithmOpt = Arrays.stream(compressionAlgorithmsOrder).sequential().filter(compressionCapabilities.get(XServerCapabilities.SUBKEY_COMPRESSION_ALGORITHM)::contains).filter(compressionAlgorithms::containsKey).findFirst();
    if (!algorithmOpt.isPresent()) {
        if (compression == Compression.REQUIRED) {
            throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("Protocol.Compression.2"));
        }
        // TODO Log "Compression negotiation failed. Connection will proceed uncompressed."
        return;
    }
    String algorithm = algorithmOpt.get();
    this.compressionAlgorithm = compressionAlgorithms.get(algorithm);
    // Make sure the picked compression algorithm streams exist.
    this.compressionAlgorithm.getInputStreamClass();
    this.compressionAlgorithm.getOutputStreamClass();
    Map<String, Object> compressionCap = new HashMap<>();
    compressionCap.put(XServerCapabilities.SUBKEY_COMPRESSION_ALGORITHM, algorithm);
    compressionCap.put(XServerCapabilities.SUBKEY_COMPRESSION_SERVER_COMBINE_MIXED_MESSAGES, true);
    sendCapabilities(Collections.singletonMap(XServerCapabilities.KEY_COMPRESSION, compressionCap));
    this.compressionEnabled = true;
}
Also used : Compression(com.mysql.cj.conf.PropertyDefinitions.Compression) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

Compression (com.mysql.cj.conf.PropertyDefinitions.Compression)3 AddResult (com.mysql.cj.xdevapi.AddResult)2 Collection (com.mysql.cj.xdevapi.Collection)2 DocResult (com.mysql.cj.xdevapi.DocResult)2 JsonString (com.mysql.cj.xdevapi.JsonString)2 Session (com.mysql.cj.xdevapi.Session)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1