jacek-lewandowski commented on code in PR #2807: URL: https://github.com/apache/cassandra/pull/2807#discussion_r1374232161
########## src/java/org/apache/cassandra/tcm/log/SystemKeyspaceStorage.java: ########## @@ -0,0 +1,168 @@ +/* + * 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.tcm.log; + +import java.nio.ByteBuffer; +import java.util.function.Supplier; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.cql3.UntypedResultSet; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.schema.SchemaConstants; +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.Transformation; + +import static org.apache.cassandra.cql3.QueryProcessor.executeInternal; +import static org.apache.cassandra.db.SystemKeyspace.LocalMetadataLog; + +/** + * {@code LogStorage} storing CMS log entries into the {@code system.local_metadata_log} table. + */ +public class SystemKeyspaceStorage implements LogStorage +{ + private static final Logger logger = LoggerFactory.getLogger(SystemKeyspaceStorage.class); + public static final String NAME = org.apache.cassandra.db.SystemKeyspace.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; + + private final Supplier<MetadataSnapshots> snapshots; + + public SystemKeyspaceStorage() + { + this(() -> ClusterMetadataService.instance().snapshotManager()); + } + + @VisibleForTesting + public SystemKeyspaceStorage(Supplier<MetadataSnapshots> snapshots) + { + this.snapshots = snapshots; + } + + // This method is always called from a single thread, so doesn't have to be synchonised. + @Override + public void append(long period, Entry entry) + { + try + { + // TODO get lowest supported metadata version from ClusterMetadata + ByteBuffer serializedTransformation = entry.transform.kind().toVersionedBytes(entry.transform); + String query = String.format("INSERT INTO %s.%s (period, epoch, current_epoch, entry_id, transformation, kind) VALUES (?,?,?,?,?,?)", + SchemaConstants.SYSTEM_KEYSPACE_NAME, NAME); + executeInternal(query, period, entry.epoch.getEpoch(), entry.epoch.getEpoch(), + entry.id.entryId, serializedTransformation, entry.transform.kind().toString()); + // todo; should probably not flush every time, but it simplifies tests + Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStore(NAME).forceBlockingFlush(ColumnFamilyStore.FlushReason.INTERNALLY_FORCED); + } + catch (Throwable t) + { + logger.error("Could not persist the entry {} proceeding with in-memory commit.", entry, t); + } + } + + /** + * Checks if the metdata log table contains at least one row. + * @return {@code true} if the metdata log table contains at least one row, {@code false} otherwise. + */ + public synchronized static boolean hasAnyEpoch() + { + String query = String.format("SELECT epoch FROM %s.%s LIMIT 1", SchemaConstants.SYSTEM_KEYSPACE_NAME, NAME); + + for (UntypedResultSet.Row row : executeInternal(query)) Review Comment: I suppose it returns null when the statement does not return rows (for example, a schema update, or grant/revoke) -- 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]

