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
SchemaUpdateHandler
obtained 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 Schema
instance
SchemaUpdateHandler
updateHandler
-
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 Keyspaces
distributedKeyspaces()
java.util.Optional<UserFunction>
findUserFunction(FunctionName name, java.util.List<AbstractType<?>> argTypes)
Find the function with the specified name and arguments.ColumnFamilyStore
getColumnFamilyStoreInstance(TableId id)
ReturnsColumnFamilyStore
by the table identifier.DistributedSchema
getDistributedSchemaBlocking()
Returns the current keyspaces metadata and version synchronouly.TableMetadataRef
getIndexTableMetadataRef(java.lang.String keyspace, java.lang.String index)
Keyspace
getKeyspaceInstance(java.lang.String keyspaceName)
Get keyspace instance by nameKeyspaceMetadata
getKeyspaceMetadata(java.lang.String keyspaceName)
Get metadata about keyspace by its namecom.google.common.collect.Sets.SetView<java.lang.String>
getKeyspaces()
Keyspaces
getLocalKeyspaces()
int
getNumberOfTables()
java.util.Map<java.util.UUID,java.util.Set<InetAddressAndPort>>
getOutstandingSchemaVersions()
TableMetadata
getTableMetadata(java.lang.String keyspace, java.lang.String table)
Given a keyspace name and table name, get the table meta data.TableMetadata
getTableMetadata(Descriptor descriptor)
TableMetadata
getTableMetadata(TableId id)
TableMetadataRef
getTableMetadataRef(java.lang.String keyspace, java.lang.String table)
Given a keyspace name and table/view name, get the table metadata reference.TableMetadataRef
getTableMetadataRef(Descriptor descriptor)
TableMetadataRef
getTableMetadataRef(TableId id)
Get Table metadata by its identifierjava.lang.Iterable<TableMetadata>
getTablesAndViews(java.lang.String keyspaceName)
Get metadata about keyspace inner ColumnFamiliesjava.util.Collection<UserFunction>
getUserFunctions(FunctionName name)
Get all user-defined function overloads with the specified name.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_NAMES
or virtual keyspaces.java.util.UUID
getVersion()
Returns the current schema version.ViewMetadata
getView(java.lang.String keyspaceName, java.lang.String viewName)
boolean
isEmpty()
Checks whether the current schema is empty.boolean
isSameVersion(java.util.UUID schemaVersion)
Checks whether the given schema version is the same as the current local schema.int
largestGcgs()
Compute the largest gc grace seconds amongst all the tablesvoid
loadFromDisk()
Load keyspaces definitions from local storage, seeSchemaUpdateHandler.reset(boolean)
.Keyspace
maybeAddKeyspaceInstance(java.lang.String keyspaceName, java.util.function.Supplier<Keyspace> loadFunction)
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)void
registerListener(SchemaChangeListener listener)
void
reloadSchemaAndAnnounceVersion()
void
resetLocalSchema()
Clear all locally stored schema information and fetch schema from another node.void
startSync()
SchemaTransformation.SchemaTransformationResult
transform(SchemaTransformation transformation)
SchemaTransformation.SchemaTransformationResult
transform(SchemaTransformation transformation, boolean local)
void
unregisterListener(SchemaChangeListener listener)
TableMetadata
validateTable(java.lang.String keyspaceName, java.lang.String tableName)
boolean
waitUntilReady(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:
getKeyspaceInstance
in interfaceSchemaProvider
- Parameters:
keyspaceName
- The name of the keyspace- Returns:
- Keyspace object or null if keyspace was not found
-
getColumnFamilyStoreInstance
public ColumnFamilyStore getColumnFamilyStoreInstance(TableId id)
ReturnsColumnFamilyStore
by the table identifier. Note that though, if called forTableMetadata.id
, when metadata points to a secondary index table, theTableMetadata.id
denotes the identifier of the main table, not the index table. Thus, this method will return CFS of the main table rather than, probably expected, CFS for the index backing table.
-
maybeAddKeyspaceInstance
public Keyspace maybeAddKeyspaceInstance(java.lang.String keyspaceName, java.util.function.Supplier<Keyspace> loadFunction)
- Specified by:
maybeAddKeyspaceInstance
in 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:
getKeyspaceMetadata
in 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_NAMES
or 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:
getTableMetadataRef
in 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:
getTableMetadataRef
in interfaceSchemaProvider
- Parameters:
id
- table or view identifier- Returns:
- metadata about Table or View
-
getTableMetadataRef
public TableMetadataRef getTableMetadataRef(Descriptor descriptor)
- Specified by:
getTableMetadataRef
in 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:
getTableMetadata
in 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:
getTableMetadata
in interfaceSchemaProvider
-
validateTable
public TableMetadata validateTable(java.lang.String keyspaceName, java.lang.String tableName)
-
getTableMetadata
public TableMetadata getTableMetadata(Descriptor descriptor)
-
getUserFunctions
public java.util.Collection<UserFunction> getUserFunctions(FunctionName name)
Get all user-defined 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
UserFunction
otherwise
-
findUserFunction
public java.util.Optional<UserFunction> findUserFunction(FunctionName name, java.util.List<AbstractType<?>> argTypes)
Find the function with the specified name and arguments.- Parameters:
name
- fully qualified function nameargTypes
- function argument types- Returns:
- an empty
Optional
if the keyspace or the function name are not found; a non-empty optional ofFunction
otherwise
-
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
Schema
orSchemaUpdateHandler
implementations. 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()
-
-