Class 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 (see mergeAndUpdateVersion(SchemaTransformationResult, boolean)).

    • Method Detail

      • startSync

        public void startSync()
      • waitUntilReady

        public boolean waitUntilReady​(java.time.Duration timeout)
      • getKeyspaceInstance

        public Keyspace getKeyspaceInstance​(java.lang.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.
      • 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 interface SchemaProvider
        Parameters:
        keyspaceName - The name of the keyspace
        Returns:
        The keyspace metadata or null if it wasn't found
      • 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 interface SchemaProvider
        Returns:
        TableMetadataRef object or null if it wasn't found
      • getIndexTableMetadataRef

        public TableMetadataRef getIndexTableMetadataRef​(java.lang.String keyspace,
                                                         java.lang.String index)
      • 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 interface SchemaProvider
        Parameters:
        keyspace - The keyspace name
        table - The table name
        Returns:
        TableMetadata object or null if it wasn't found
      • validateTable

        public TableMetadata validateTable​(java.lang.String keyspaceName,
                                           java.lang.String tableName)
      • 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 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 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 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​(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 - 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
      • 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()