Class Schema

java.lang.Object
org.apache.cassandra.schema.Schema
All Implemented Interfaces:
SchemaProvider

public class Schema extends 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 (see mergeAndUpdateVersion(SchemaTransformationResult, boolean)).

  • Field Details

  • Constructor Details

  • Method Details

    • startSync

      public void startSync()
    • waitUntilReady

      public boolean waitUntilReady(Duration timeout)
    • loadFromDisk

      public void loadFromDisk()
      Load keyspaces definitions from local storage, see SchemaUpdateHandler.reset(boolean).
    • registerListener

      public void registerListener(SchemaChangeListener listener)
    • unregisterListener

      public void unregisterListener(SchemaChangeListener listener)
    • getKeyspaceInstance

      public Keyspace getKeyspaceInstance(String keyspaceName)
      Get keyspace instance by name
      Specified by:
      getKeyspaceInstance in interface SchemaProvider
      Parameters:
      keyspaceName - The name of the keyspace
      Returns:
      Keyspace object or null if keyspace was not found
    • getColumnFamilyStoreInstance

      public ColumnFamilyStore getColumnFamilyStoreInstance(TableId id)
      Returns ColumnFamilyStore by the table identifier. Note that though, if called for TableMetadata.id, when metadata points to a secondary index table, the TableMetadata.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(String keyspaceName, Supplier<Keyspace> loadFunction)
      Specified by:
      maybeAddKeyspaceInstance in interface SchemaProvider
    • 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(String keyspaceName, String viewName)
    • getKeyspaceMetadata

      public KeyspaceMetadata getKeyspaceMetadata(String keyspaceName)
      Get metadata about keyspace by its name
      Specified by:
      getKeyspaceMetadata in interface SchemaProvider
      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<String> getUserKeyspaces()
      Returns user keyspaces, that is all but SchemaConstants.LOCAL_SYSTEM_KEYSPACE_NAMES, SchemaConstants.REPLICATED_SYSTEM_KEYSPACE_NAMES or virtual keyspaces.
    • getTablesAndViews

      public Iterable<TableMetadata> getTablesAndViews(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<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(String keyspace, 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 interface SchemaProvider
      Returns:
      TableMetadataRef object or null if it wasn't found
    • getIndexTableMetadataRef

      public TableMetadataRef getIndexTableMetadataRef(String keyspace, String index)
    • getTableMetadataRef

      public TableMetadataRef getTableMetadataRef(TableId id)
      Get Table metadata by its identifier
      Specified by:
      getTableMetadataRef in interface SchemaProvider
      Parameters:
      id - table or view identifier
      Returns:
      metadata about Table or View
    • getTableMetadataRef

      public TableMetadataRef getTableMetadataRef(Descriptor descriptor)
      Specified by:
      getTableMetadataRef in interface SchemaProvider
    • getTableMetadata

      public TableMetadata getTableMetadata(String keyspace, 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 interface SchemaProvider
      Parameters:
      keyspace - The keyspace name
      table - The table name
      Returns:
      TableMetadata object or null if it wasn't found
    • getTableMetadata

      public TableMetadata getTableMetadata(TableId id)
      Specified by:
      getTableMetadata in interface SchemaProvider
    • validateTable

      public TableMetadata validateTable(String keyspaceName, String tableName)
    • getTableMetadata

      public TableMetadata getTableMetadata(Descriptor descriptor)
    • getUserFunctions

      public 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 Optional<UserFunction> findUserFunction(FunctionName name, List<AbstractType<?>> argTypes)
      Find the function with the specified name and arguments.
      Parameters:
      name - fully qualified function name
      argTypes - function argument types
      Returns:
      an empty Optional if the keyspace or the function name are not found; a non-empty optional of Function otherwise
    • getVersion

      public 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 or SchemaUpdateHandler implementations. Please use getDistributedSchemaBlocking() 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(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 - see getVersion().
    • 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 - see getVersion().
    • 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

    • 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 Map<UUID,Set<InetAddressAndPort>> getOutstandingSchemaVersions()