jacek-lewandowski commented on code in PR #2807:
URL: https://github.com/apache/cassandra/pull/2807#discussion_r1366933312


##########
src/java/org/apache/cassandra/schema/DistributedMetadataLogKeyspace.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.schema;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSortedSet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.cql3.UntypedResultSet;
+import org.apache.cassandra.cql3.statements.schema.CreateTableStatement;
+import org.apache.cassandra.db.ConsistencyLevel;
+import org.apache.cassandra.exceptions.CasWriteTimeoutException;
+import org.apache.cassandra.metrics.TCMMetrics;
+import org.apache.cassandra.tcm.ClusterMetadataService;
+import org.apache.cassandra.tcm.Epoch;
+import org.apache.cassandra.tcm.MetadataSnapshots;
+import org.apache.cassandra.tcm.Period;
+import org.apache.cassandra.tcm.Retry;
+import org.apache.cassandra.tcm.Transformation;
+import org.apache.cassandra.tcm.log.Entry;
+import org.apache.cassandra.tcm.log.LogReader;
+import org.apache.cassandra.tcm.log.LogState;
+import org.apache.cassandra.tcm.log.Replication;
+import org.apache.cassandra.tcm.transformations.cms.PreInitialize;
+import org.apache.cassandra.utils.JVMStabilityInspector;
+
+import static org.apache.cassandra.tcm.Epoch.FIRST;
+
+public final class DistributedMetadataLogKeyspace
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(DistributedMetadataLogKeyspace.class);
+
+    private DistributedMetadataLogKeyspace(){}
+
+    public static final String TABLE_NAME = "distributed_metadata_log";
+
+    /**
+     * Generation is used as a timestamp for automatic table creation on 
startup.
+     * If you make any changes to the tables below, make sure to increment the
+     * generation and document your change here.
+     * <p>
+     * gen 0: original definition in 5.0
+     */
+    public static final long GENERATION = 0;
+
+    public static final String LOG_TABLE_CQL = "CREATE TABLE %s.%s ("
+                                               + "period bigint,"
+                                               + "current_epoch bigint static,"
+                                               + "sealed boolean static,"
+                                               + "epoch bigint,"
+                                               + "entry_id bigint,"
+                                               + "transformation blob,"
+                                               + "kind text,"
+                                               + "PRIMARY KEY (period, 
epoch))";
+
+    public static final TableMetadata Log =
+        parse(LOG_TABLE_CQL, TABLE_NAME, "Log")
+        
.compaction(CompactionParams.twcs(ImmutableMap.of("compaction_window_unit","DAYS",
+                                                          
"compaction_window_size","1")))
+        .build();
+
+    public static boolean initialize() throws IOException
+    {
+        try
+        {
+            String init = String.format("INSERT INTO %s.%s (period, epoch, 
current_epoch, transformation, kind, entry_id, sealed) " +
+                                        "VALUES(?, ?, ?, ?, ?, ?, false) " +
+                                        "IF NOT EXISTS", 
SchemaConstants.METADATA_KEYSPACE_NAME, TABLE_NAME);
+            UntypedResultSet result = QueryProcessor.execute(init, 
ConsistencyLevel.QUORUM,
+                                                             Period.FIRST, 
FIRST.getEpoch(), FIRST.getEpoch(),
+                                                             
Transformation.Kind.PRE_INITIALIZE_CMS.toVersionedBytes(PreInitialize.blank()), 
Transformation.Kind.PRE_INITIALIZE_CMS.toString(), Entry.Id.NONE.entryId);
+
+            return result.one().getBoolean("[applied]");
+        }
+        catch (CasWriteTimeoutException t)
+        {
+            logger.warn("Timed out while trying to CAS", t);
+            return false;
+        }
+        catch (Throwable t)
+        {
+            logger.error("Caught an exception while trying to CAS", t);
+            return false;
+        }
+    }
+
+    public static boolean tryCommit(Entry.Id entryId,
+                                    Transformation transform,
+                                    Epoch previousEpoch,
+                                    Epoch nextEpoch,
+                                    long previousPeriod,
+                                    long nextPeriod,
+                                    boolean sealCurrentPeriod)
+    {
+        try
+        {
+            if (previousEpoch.is(FIRST) && !initialize())
+                return false;
+
+            // TODO get lowest supported metadata version from ClusterMetadata
+            ByteBuffer serializedEvent = 
transform.kind().toVersionedBytes(transform);
+
+            UntypedResultSet result;
+            if (previousPeriod + 1 == nextPeriod || 
ClusterMetadataService.state() == ClusterMetadataService.State.RESET)
+            {
+                String query = String.format("INSERT INTO %s.%s (period, 
epoch, current_epoch, entry_id, transformation, kind, sealed) " +
+                                             "VALUES (?, ?, ?, ?, ?, ?, false) 
" +
+                                             "IF NOT EXISTS;",
+                                             
SchemaConstants.METADATA_KEYSPACE_NAME, TABLE_NAME);
+                result = QueryProcessor.execute(query, ConsistencyLevel.QUORUM,
+                                                nextPeriod, 
nextEpoch.getEpoch(), nextEpoch.getEpoch(), entryId.entryId, serializedEvent, 
transform.kind().toString());
+            }
+            else
+            {
+                assert previousPeriod == nextPeriod;
+                String query = String.format("UPDATE %s.%s SET current_epoch = 
?, sealed = ?, entry_id = ?, transformation = ?, kind = ? " +

Review Comment:
   What are the cases when the log entry is overwritten?



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to