You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by ma...@apache.org on 2024/04/12 06:31:58 UTC
(cassandra) branch trunk updated: Use Transformation.Kind.id in local and distributed log tables
This is an automated email from the ASF dual-hosted git repository.
marcuse pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git
The following commit(s) were added to refs/heads/trunk by this push:
new d548396597 Use Transformation.Kind.id in local and distributed log tables
d548396597 is described below
commit d548396597897012c0b8ed83264f8dd911fb84bf
Author: Marcus Eriksson <ma...@apache.org>
AuthorDate: Wed Apr 3 09:58:49 2024 +0200
Use Transformation.Kind.id in local and distributed log tables
Patch by Sam Tunnicliffe and marcuse; reviewed by Sam Tunnicliffe for CASSANDRA-19516
Co-authored-by: Sam Tunnicliffe <sa...@apache.org>
Co-authored-by: Marcus Eriksson <ma...@apache.org>
---
CHANGES.txt | 1 +
.../cql3/functions/ClusterMetadataFcts.java | 57 ++++++++++++++++++
.../cassandra/cql3/functions/NativeFunctions.java | 1 +
.../org/apache/cassandra/db/SystemKeyspace.java | 2 +-
.../db/virtual/ClusterMetadataLogTable.java | 2 +-
.../schema/DistributedMetadataLogKeyspace.java | 10 ++--
.../org/apache/cassandra/tcm/Transformation.java | 2 +-
.../cassandra/tcm/log/SystemKeyspaceStorage.java | 4 +-
.../cql3/functions/ClusterMetadataFctsTest.java | 68 ++++++++++++++++++++++
9 files changed, 137 insertions(+), 10 deletions(-)
diff --git a/CHANGES.txt b/CHANGES.txt
index c313028dad..cfbbc651ef 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
5.1
+ * Use Transformation.Kind.id in local and distributed log tables (CASSANDRA-19516)
* Remove period field from ClusterMetadata and metadata log tables (CASSANDRA-19482)
* Enrich system_views.pending_hints vtable with hints sizes (CASSANDRA-19486)
* Expose all dropwizard metrics in virtual tables (CASSANDRA-14572)
diff --git a/src/java/org/apache/cassandra/cql3/functions/ClusterMetadataFcts.java b/src/java/org/apache/cassandra/cql3/functions/ClusterMetadataFcts.java
new file mode 100644
index 0000000000..5c209aaa5e
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/functions/ClusterMetadataFcts.java
@@ -0,0 +1,57 @@
+/*
+ * 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.cql3.functions;
+
+import java.nio.ByteBuffer;
+
+import org.apache.cassandra.db.marshal.Int32Type;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.tcm.Transformation;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+public class ClusterMetadataFcts
+{
+ public static void addFunctionsTo(NativeFunctions functions)
+ {
+ functions.add(transformationKind);
+ }
+
+ public static final NativeScalarFunction transformationKind = new TransformationKind();
+ private static final class TransformationKind extends NativeScalarFunction
+ {
+
+ private TransformationKind()
+ {
+ super("transformation_kind", UTF8Type.instance, Int32Type.instance);
+ }
+
+ @Override
+ public ByteBuffer execute(Arguments arguments) throws InvalidRequestException
+ {
+ Number id = arguments.get(0);
+ if (id.intValue() < 0 || id.intValue() > Transformation.Kind.values().length -1)
+ throw new InvalidRequestException(id + " is not a valid Transformation.Kind id");
+
+ Transformation.Kind kind = Transformation.Kind.fromId(id.intValue());
+ return ByteBufferUtil.bytes(kind.name());
+
+ }
+ }
+}
diff --git a/src/java/org/apache/cassandra/cql3/functions/NativeFunctions.java b/src/java/org/apache/cassandra/cql3/functions/NativeFunctions.java
index 2100fe3f89..a9deba5cb1 100644
--- a/src/java/org/apache/cassandra/cql3/functions/NativeFunctions.java
+++ b/src/java/org/apache/cassandra/cql3/functions/NativeFunctions.java
@@ -47,6 +47,7 @@ public class NativeFunctions
MathFcts.addFunctionsTo(this);
MaskingFcts.addFunctionsTo(this);
VectorFcts.addFunctionsTo(this);
+ ClusterMetadataFcts.addFunctionsTo(this);
}
};
diff --git a/src/java/org/apache/cassandra/db/SystemKeyspace.java b/src/java/org/apache/cassandra/db/SystemKeyspace.java
index 4e2f8aa933..e7840a29e3 100644
--- a/src/java/org/apache/cassandra/db/SystemKeyspace.java
+++ b/src/java/org/apache/cassandra/db/SystemKeyspace.java
@@ -489,7 +489,7 @@ public final class SystemKeyspace
+ "epoch bigint,"
+ "entry_id bigint,"
+ "transformation blob,"
- + "kind text,"
+ + "kind int,"
+ "PRIMARY KEY (epoch))")
.partitioner(MetaStrategy.partitioner)
.compaction(CompactionParams.twcs(ImmutableMap.of("compaction_window_unit","DAYS",
diff --git a/src/java/org/apache/cassandra/db/virtual/ClusterMetadataLogTable.java b/src/java/org/apache/cassandra/db/virtual/ClusterMetadataLogTable.java
index 1c345a19b4..152b4769a7 100644
--- a/src/java/org/apache/cassandra/db/virtual/ClusterMetadataLogTable.java
+++ b/src/java/org/apache/cassandra/db/virtual/ClusterMetadataLogTable.java
@@ -66,7 +66,7 @@ final class ClusterMetadataLogTable extends AbstractVirtualTable
"FROM %s.%s", METADATA_KEYSPACE_NAME, TABLE_NAME), ConsistencyLevel.QUORUM);
for (UntypedResultSet.Row r : res)
{
- Transformation.Kind kind = Transformation.Kind.valueOf(r.getString("kind"));
+ Transformation.Kind kind = Transformation.Kind.fromId(r.getInt("kind"));
Transformation transformation = kind.fromVersionedBytes(r.getBlob("transformation"));
result.row(r.getLong("epoch"))
diff --git a/src/java/org/apache/cassandra/schema/DistributedMetadataLogKeyspace.java b/src/java/org/apache/cassandra/schema/DistributedMetadataLogKeyspace.java
index 896f2bf51b..4607a0fcf5 100644
--- a/src/java/org/apache/cassandra/schema/DistributedMetadataLogKeyspace.java
+++ b/src/java/org/apache/cassandra/schema/DistributedMetadataLogKeyspace.java
@@ -68,7 +68,7 @@ public final class DistributedMetadataLogKeyspace
+ "epoch bigint,"
+ "entry_id bigint,"
+ "transformation blob,"
- + "kind text,"
+ + "kind int,"
+ "PRIMARY KEY (epoch))";
public static final TableMetadata Log =
@@ -88,7 +88,7 @@ public final class DistributedMetadataLogKeyspace
UntypedResultSet result = QueryProcessor.execute(init, ConsistencyLevel.QUORUM,
FIRST.getEpoch(),
Transformation.Kind.PRE_INITIALIZE_CMS.toVersionedBytes(PreInitialize.blank()),
- Transformation.Kind.PRE_INITIALIZE_CMS.toString(),
+ Transformation.Kind.PRE_INITIALIZE_CMS.id,
Entry.Id.NONE.entryId);
UntypedResultSet.Row row = result.one();
@@ -97,7 +97,7 @@ public final class DistributedMetadataLogKeyspace
if (row.getLong("epoch") == FIRST.getEpoch() &&
row.getLong("entry_id") == Entry.Id.NONE.entryId &&
- Transformation.Kind.PRE_INITIALIZE_CMS.toString().equals(row.getString("kind")))
+ Transformation.Kind.PRE_INITIALIZE_CMS.id == row.getInt("kind"))
return true;
throw new IllegalStateException("Could not initialize log.");
@@ -137,7 +137,7 @@ public final class DistributedMetadataLogKeyspace
nextEpoch.getEpoch(),
entryId.entryId,
serializedEvent,
- transform.kind().toString());
+ transform.kind().id);
return result.one().getBoolean("[applied]");
}
@@ -191,7 +191,7 @@ public final class DistributedMetadataLogKeyspace
{
long entryId = row.getLong("entry_id");
Epoch epoch = Epoch.create(row.getLong("epoch"));
- Transformation.Kind kind = Transformation.Kind.valueOf(row.getString("kind"));
+ Transformation.Kind kind = Transformation.Kind.fromId(row.getInt("kind"));
Transformation transform = kind.fromVersionedBytes(row.getBlob("transformation"));
entryHolder.add(new Entry(new Entry.Id(entryId), epoch, transform));
}
diff --git a/src/java/org/apache/cassandra/tcm/Transformation.java b/src/java/org/apache/cassandra/tcm/Transformation.java
index 8432d136b4..e4b81bc765 100644
--- a/src/java/org/apache/cassandra/tcm/Transformation.java
+++ b/src/java/org/apache/cassandra/tcm/Transformation.java
@@ -217,7 +217,7 @@ public interface Transformation
;
private final Supplier<AsymmetricMetadataSerializer<Transformation, ? extends Transformation>> serializer;
- private final int id;
+ public final int id;
private static final Kind[] idToKindMap;
diff --git a/src/java/org/apache/cassandra/tcm/log/SystemKeyspaceStorage.java b/src/java/org/apache/cassandra/tcm/log/SystemKeyspaceStorage.java
index ac419b28ff..a0f6263e1c 100644
--- a/src/java/org/apache/cassandra/tcm/log/SystemKeyspaceStorage.java
+++ b/src/java/org/apache/cassandra/tcm/log/SystemKeyspaceStorage.java
@@ -75,7 +75,7 @@ public class SystemKeyspaceStorage implements LogStorage
ByteBuffer serializedTransformation = entry.transform.kind().toVersionedBytes(entry.transform);
String query = String.format("INSERT INTO %s.%s (epoch, entry_id, transformation, kind) VALUES (?,?,?,?)",
SchemaConstants.SYSTEM_KEYSPACE_NAME, NAME);
- executeInternal(query, entry.epoch.getEpoch(), entry.id.entryId, serializedTransformation, entry.transform.kind().toString());
+ executeInternal(query, entry.epoch.getEpoch(), entry.id.entryId, serializedTransformation, entry.transform.kind().id);
// todo; should probably not flush every time, but it simplifies tests
Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStore(NAME).forceBlockingFlush(ColumnFamilyStore.FlushReason.INTERNALLY_FORCED);
}
@@ -149,7 +149,7 @@ public class SystemKeyspaceStorage implements LogStorage
{
long entryId = row.getLong("entry_id");
Epoch epoch = Epoch.create(row.getLong("epoch"));
- Transformation.Kind kind = Transformation.Kind.valueOf(row.getString("kind"));
+ Transformation.Kind kind = Transformation.Kind.fromId(row.getInt("kind"));
Transformation transform = kind.fromVersionedBytes(row.getBlob("transformation"));
holder.add(new Entry(new Entry.Id(entryId), epoch, transform));
}
diff --git a/test/unit/org/apache/cassandra/cql3/functions/ClusterMetadataFctsTest.java b/test/unit/org/apache/cassandra/cql3/functions/ClusterMetadataFctsTest.java
new file mode 100644
index 0000000000..408889e088
--- /dev/null
+++ b/test/unit/org/apache/cassandra/cql3/functions/ClusterMetadataFctsTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.cql3.functions;
+
+import java.nio.charset.CharacterCodingException;
+
+import org.junit.Test;
+
+import org.apache.cassandra.db.marshal.Int32Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.tcm.Transformation;
+import org.apache.cassandra.transport.ProtocolVersion;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.assertj.core.api.Assertions;
+
+import static org.apache.cassandra.cql3.functions.ClusterMetadataFcts.transformationKind;
+import static org.junit.Assert.assertEquals;
+
+public class ClusterMetadataFctsTest
+{
+
+ @Test
+ public void testTransformationKind() throws CharacterCodingException
+ {
+ int max = -1;
+ for (Transformation.Kind kind : Transformation.Kind.values())
+ {
+ Arguments arguments = transformationKind.newArguments(ProtocolVersion.CURRENT);
+ arguments.set(0, Int32Type.instance.decompose(kind.id));
+ assertEquals(kind.name(), ByteBufferUtil.string(transformationKind.execute(arguments)));
+ if (kind.id > max)
+ max = kind.id;
+ }
+
+ for (int boundary : new int[]{-1, max+1})
+ {
+ Arguments arguments = transformationKind.newArguments(ProtocolVersion.CURRENT);
+ arguments.set(0, Int32Type.instance.decompose(boundary));
+ try
+ {
+ transformationKind.execute(arguments);
+ }
+ catch (Exception e)
+ {
+ Assertions.assertThat(e)
+ .isInstanceOf(InvalidRequestException.class)
+ .hasMessageContaining(boundary + " is not a valid Transformation.Kind id");
+ }
+ }
+
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org