Class Schema
- java.lang.Object
-
- org.apache.cassandra.schema.Schema
-
- All Implemented Interfaces:
SchemaProvider
public class Schema extends java.lang.Object implements SchemaProvider
Manages shared schema, keyspace instances and table metadata refs. Provides methods to initialize, modify and query both the shared and local schema, as well as to register listeners.This class should be the only entity used to query and manage schema. Internal details should not be access in production code (would be great if they were not accessed in the test code as well).
TL;DR: All modifications are made using the implementation of
SchemaUpdateHandlerobtained from the provided factory. After each modification, the internally managed table metadata refs and keyspaces instances are updated and notifications are sent to the registered listeners. When the schema change is applied by the update handler (regardless it is initiated locally or received from outside), the registered callback is executed which performs the remaining updates for tables metadata refs and keyspace instances (seemergeAndUpdateVersion(SchemaTransformationResult, boolean)).
-
-
Field Summary
Fields Modifier and Type Field Description static SchemainstanceSchemaUpdateHandlerupdateHandler
-
Constructor Summary
Constructors Constructor Description Schema(boolean online, Keyspaces localKeyspaces, SchemaUpdateHandler updateHandler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description KeyspacesdistributedKeyspaces()java.util.Optional<Function>findFunction(FunctionName name, java.util.List<AbstractType<?>> argTypes)Find the function with the specified nameColumnFamilyStoregetColumnFamilyStoreInstance(TableId id)DistributedSchemagetDistributedSchemaBlocking()Returns the current keyspaces metadata and version synchronouly.java.util.Collection<Function>getFunctions(FunctionName name)Get all function overloads with the specified nameTableMetadataRefgetIndexTableMetadataRef(java.lang.String keyspace, java.lang.String index)KeyspacegetKeyspaceInstance(java.lang.String keyspaceName)Get keyspace instance by nameKeyspaceMetadatagetKeyspaceMetadata(java.lang.String keyspaceName)Get metadata about keyspace by its namecom.google.common.collect.Sets.SetView<java.lang.String>getKeyspaces()KeyspacesgetLocalKeyspaces()intgetNumberOfTables()java.util.Map<java.util.UUID,java.util.Set<InetAddressAndPort>>getOutstandingSchemaVersions()TableMetadatagetTableMetadata(java.lang.String keyspace, java.lang.String table)Given a keyspace name and table name, get the table meta data.TableMetadatagetTableMetadata(Descriptor descriptor)TableMetadatagetTableMetadata(TableId id)TableMetadataRefgetTableMetadataRef(java.lang.String keyspace, java.lang.String table)Given a keyspace name and table/view name, get the table metadata reference.TableMetadataRefgetTableMetadataRef(Descriptor descriptor)TableMetadataRefgetTableMetadataRef(TableId id)Get Table metadata by its identifierjava.lang.Iterable<TableMetadata>getTablesAndViews(java.lang.String keyspaceName)Get metadata about keyspace inner ColumnFamiliescom.google.common.collect.Sets.SetView<java.lang.String>getUserKeyspaces()Returns user keyspaces, that is all butSchemaConstants.LOCAL_SYSTEM_KEYSPACE_NAMES,SchemaConstants.REPLICATED_SYSTEM_KEYSPACE_NAMESor virtual keyspaces.java.util.UUIDgetVersion()Returns the current schema version.ViewMetadatagetView(java.lang.String keyspaceName, java.lang.String viewName)booleanisEmpty()Checks whether the current schema is empty.booleanisSameVersion(java.util.UUID schemaVersion)Checks whether the given schema version is the same as the current local schema.intlargestGcgs()Compute the largest gc grace seconds amongst all the tablesvoidloadFromDisk()Load keyspaces definitions from local storage, seeSchemaUpdateHandler.reset(boolean).KeyspacemaybeAddKeyspaceInstance(java.lang.String keyspaceName, java.util.function.Supplier<Keyspace> loadFunction)voidmergeAndUpdateVersion(SchemaTransformation.SchemaTransformationResult result, boolean dropData)Merge remote schema in form of mutations with local and mutate ks/cf metadata objects (which also involves fs operations on add/drop ks/cf)voidregisterListener(SchemaChangeListener listener)voidreloadSchemaAndAnnounceVersion()voidresetLocalSchema()Clear all locally stored schema information and fetch schema from another node.voidstartSync()SchemaTransformation.SchemaTransformationResulttransform(SchemaTransformation transformation)SchemaTransformation.SchemaTransformationResulttransform(SchemaTransformation transformation, boolean local)voidunregisterListener(SchemaChangeListener listener)TableMetadatavalidateTable(java.lang.String keyspaceName, java.lang.String tableName)booleanwaitUntilReady(java.time.Duration timeout)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.cassandra.schema.SchemaProvider
getExistingTableMetadata
-
-
-
-
Field Detail
-
instance
public static final Schema instance
-
updateHandler
public final SchemaUpdateHandler updateHandler
-
-
Constructor Detail
-
Schema
public Schema(boolean online, Keyspaces localKeyspaces, SchemaUpdateHandler updateHandler)
-
-
Method Detail
-
startSync
public void startSync()
-
waitUntilReady
public boolean waitUntilReady(java.time.Duration timeout)
-
loadFromDisk
public void loadFromDisk()
Load keyspaces definitions from local storage, seeSchemaUpdateHandler.reset(boolean).
-
registerListener
public void registerListener(SchemaChangeListener listener)
-
unregisterListener
public void unregisterListener(SchemaChangeListener listener)
-
getKeyspaceInstance
public Keyspace getKeyspaceInstance(java.lang.String keyspaceName)
Get keyspace instance by name- Specified by:
getKeyspaceInstancein interfaceSchemaProvider- Parameters:
keyspaceName- The name of the keyspace- Returns:
- Keyspace object or null if keyspace was not found
-
getColumnFamilyStoreInstance
public ColumnFamilyStore getColumnFamilyStoreInstance(TableId id)
-
maybeAddKeyspaceInstance
public Keyspace maybeAddKeyspaceInstance(java.lang.String keyspaceName, java.util.function.Supplier<Keyspace> loadFunction)
- Specified by:
maybeAddKeyspaceInstancein interfaceSchemaProvider
-
distributedKeyspaces
public Keyspaces distributedKeyspaces()
-
largestGcgs
public int largestGcgs()
Compute the largest gc grace seconds amongst all the tables- Returns:
- the largest gcgs.
-
getNumberOfTables
public int getNumberOfTables()
-
getView
public ViewMetadata getView(java.lang.String keyspaceName, java.lang.String viewName)
-
getKeyspaceMetadata
public KeyspaceMetadata getKeyspaceMetadata(java.lang.String keyspaceName)
Get metadata about keyspace by its name- Specified by:
getKeyspaceMetadatain interfaceSchemaProvider- Parameters:
keyspaceName- The name of the keyspace- Returns:
- The keyspace metadata or null if it wasn't found
-
getUserKeyspaces
public com.google.common.collect.Sets.SetView<java.lang.String> getUserKeyspaces()
Returns user keyspaces, that is all butSchemaConstants.LOCAL_SYSTEM_KEYSPACE_NAMES,SchemaConstants.REPLICATED_SYSTEM_KEYSPACE_NAMESor virtual keyspaces.
-
getTablesAndViews
public java.lang.Iterable<TableMetadata> getTablesAndViews(java.lang.String keyspaceName)
Get metadata about keyspace inner ColumnFamilies- Parameters:
keyspaceName- The name of the keyspace- Returns:
- metadata about ColumnFamilies the belong to the given keyspace
-
getKeyspaces
public com.google.common.collect.Sets.SetView<java.lang.String> getKeyspaces()
- Returns:
- a set of local and distributed keyspace names; it does not include virtual keyspaces
-
getLocalKeyspaces
public Keyspaces getLocalKeyspaces()
-
getTableMetadataRef
public TableMetadataRef getTableMetadataRef(java.lang.String keyspace, java.lang.String table)
Given a keyspace name and table/view name, get the table metadata reference. If the keyspace name or table/view name is not present this method returns null.- Specified by:
getTableMetadataRefin interfaceSchemaProvider- Returns:
- TableMetadataRef object or null if it wasn't found
-
getIndexTableMetadataRef
public TableMetadataRef getIndexTableMetadataRef(java.lang.String keyspace, java.lang.String index)
-
getTableMetadataRef
public TableMetadataRef getTableMetadataRef(TableId id)
Get Table metadata by its identifier- Specified by:
getTableMetadataRefin interfaceSchemaProvider- Parameters:
id- table or view identifier- Returns:
- metadata about Table or View
-
getTableMetadataRef
public TableMetadataRef getTableMetadataRef(Descriptor descriptor)
- Specified by:
getTableMetadataRefin interfaceSchemaProvider
-
getTableMetadata
public TableMetadata getTableMetadata(java.lang.String keyspace, java.lang.String table)
Given a keyspace name and table name, get the table meta data. If the keyspace name or table name is not valid this function returns null.- Specified by:
getTableMetadatain interfaceSchemaProvider- Parameters:
keyspace- The keyspace nametable- The table name- Returns:
- TableMetadata object or null if it wasn't found
-
getTableMetadata
public TableMetadata getTableMetadata(TableId id)
- Specified by:
getTableMetadatain interfaceSchemaProvider
-
validateTable
public TableMetadata validateTable(java.lang.String keyspaceName, java.lang.String tableName)
-
getTableMetadata
public TableMetadata getTableMetadata(Descriptor descriptor)
-
getFunctions
public java.util.Collection<Function> getFunctions(FunctionName name)
Get all function overloads with the specified name- Parameters:
name- fully qualified function name- Returns:
- an empty list if the keyspace or the function name are not found;
a non-empty collection of
Functionotherwise
-
findFunction
public java.util.Optional<Function> findFunction(FunctionName name, java.util.List<AbstractType<?>> argTypes)
Find the function with the specified name- Parameters:
name- fully qualified function nameargTypes- function argument types- Returns:
- an empty
Optionalif the keyspace or the function name are not found; a non-empty optional ofFunctionotherwise
-
getVersion
public java.util.UUID getVersion()
Returns the current schema version. Although, if the schema is being updated while the method was called, it can return a stale version which does not correspond to the current keyspaces metadata. It is because the schema version is unknown for the partially applied changes and is updated after the entire schema change is completed.This method should be used only internally by
SchemaorSchemaUpdateHandlerimplementations. Please usegetDistributedSchemaBlocking()to get schema version consistently in other cases.
-
getDistributedSchemaBlocking
public DistributedSchema getDistributedSchemaBlocking()
Returns the current keyspaces metadata and version synchronouly. If the schema is in the middle of a multistep transformation, the method blocks until the update is completed.
-
isSameVersion
public boolean isSameVersion(java.util.UUID schemaVersion)
Checks whether the given schema version is the same as the current local schema. Note that this method is non-blocking and may use a stale schema version for comparison - seegetVersion().
-
isEmpty
public boolean isEmpty()
Checks whether the current schema is empty. Note that this method is non-blocking and may use a stale schema version for comparison - seegetVersion().
-
reloadSchemaAndAnnounceVersion
public void reloadSchemaAndAnnounceVersion()
-
mergeAndUpdateVersion
public void mergeAndUpdateVersion(SchemaTransformation.SchemaTransformationResult result, boolean dropData)
Merge remote schema in form of mutations with local and mutate ks/cf metadata objects (which also involves fs operations on add/drop ks/cf)- Throws:
ConfigurationException- If one of metadata attributes has invalid value
-
transform
public SchemaTransformation.SchemaTransformationResult transform(SchemaTransformation transformation)
-
transform
public SchemaTransformation.SchemaTransformationResult transform(SchemaTransformation transformation, boolean local)
-
resetLocalSchema
public void resetLocalSchema()
Clear all locally stored schema information and fetch schema from another node. Called by user (via JMX) who wants to get rid of schema disagreement.
-
getOutstandingSchemaVersions
public java.util.Map<java.util.UUID,java.util.Set<InetAddressAndPort>> getOutstandingSchemaVersions()
-
-