New

Getting Started with Fusion SQL

Notebook


SingleStore Notebooks

Getting Started with Fusion SQL

In this notebook, we introduce Fusion SQL. Fusion SQL is a set of SQL statements that can be used to manage clusters, files in cluster Stage, and other resources that could previously only be managed in the portal user interface or the Management REST API.

Displaying available Fusion SQL commands

We can use the SHOW FUSION COMMANDS statement to get all of the available commands.

In [1]:

1commands = %sql SHOW FUSION COMMANDS2for cmd in commands:3    print(*cmd, '\n')

The SHOW FUSION COMMANDS also has a LIKE option that can be used to filter the displayed commands.

In [2]:

1commands = %sql SHOW FUSION COMMANDS LIKE '%stage%'2for cmd in commands:3    print(*cmd, '\n')

Let's try a workflow that goes through the entire process of creating clusters, connecting to one, and terminating them.

Working with clusters

In this example, we will create two clusters, demonstrate how to suspend and resume one of them, and then terminate them all from SQL!

A cluster carries both its compute settings and its deployment-wide settings, such as the firewall, on the one object, and every command that operates on a cluster names it directly.

Shared-tier deployments have their own set of commands — SHOW STARTER CLUSTERS, CREATE STARTER CLUSTER and DROP STARTER CLUSTER — which are not covered here.

Looking above at our list of printed commands, we see that the CREATE CLUSTER command has the following options:

CREATE CLUSTER [ IF NOT EXISTS ] '<cluster-name>'
    IN REGION '<region-name>'
    [ USING PROVIDER '<provider>' ]
    [ IN PROJECT { ID '<project-id>' | '<project-name>' } ]
    [ WITH SIZE '<size>' ]
    [ USING SCALE FACTOR <number> ]
    [ WITH FIREWALL RANGES '<ip-range>',... ]
    [ ALLOW ALL TRAFFIC ]
    [ EXPIRES AT '<iso-datetime-or-interval>' ]
    [ WAIT ON ACTIVE ];

We need a region to create a cluster in, and there is a Fusion command that gives us all of the region information. We can use this to get a region from the US by using the LIKE parameter.

SHOW CLUSTER REGIONS [ LIKE '<pattern>' ] [ ORDER BY '<key>' [ ASC | DESC ],... ] [ LIMIT <integer> ];

A region is identified by its cloud provider and its name. Name is the display name, for example US East 1 (N. Virginia), and RegionName is the cloud provider's own name for it, for example us-east-1. Either may be given to IN REGION, and USING PROVIDER picks between providers that offer a region under the same name.

In [3]:

1us_regions = %sql SHOW CLUSTER REGIONS LIKE '%US%'2us_regions

Let's use the random package to choose a US region for us. Since a region name can be ambiguous across cloud providers, we keep the provider as well and pass both to CREATE CLUSTER.

In [4]:

1import random2
3region = random.choice(us_regions)4
5region_name = region.Name6provider = region.Provider7
8region_name, provider

Creating clusters

Now that we have a region, we can create our clusters. Let's create two of different sizes and open the firewall so they can be accessed from anywhere. The size and the firewall are both clauses of CREATE CLUSTER, along with the scale factor, the expiration and the project, as shown in the syntax above.

Two things to know before running this. A cluster name is restricted to 1-32 characters of lowercase letters, digits and hyphens, and must start and end with a letter or digit. And the admin password is generated by the API and reported only at the moment the cluster is created — CREATE CLUSTER returns it in a row, so we capture that row. A cluster created without capturing it has no reachable admin user.

In [5]:

1res_1 = %sql CREATE CLUSTER 'fusion-cluster-1' IN REGION '{{ region_name }}' USING PROVIDER '{{ provider }}' WITH SIZE 'S-00' ALLOW ALL TRAFFIC2res_2 = %sql CREATE CLUSTER 'fusion-cluster-2' IN REGION '{{ region_name }}' USING PROVIDER '{{ provider }}' WITH SIZE 'S-1' ALLOW ALL TRAFFIC3
4cluster_1, cluster_2 = res_1[0], res_2[0]5
6cluster_1.Name, cluster_2.Name

If you are in the SingleStore Cloud portal, you should see the clusters displayed in a few seconds. You can also use the SHOW CLUSTERS command to list them.

In [6]:

1%%sql2SHOW CLUSTERS LIKE 'fusion-cluster-%'

Waiting for the clusters to become active

The clusters will take some time to become available. We can write a small wait loop to block until they are both ready. You could use the WAIT ON ACTIVE option for CREATE CLUSTER, but that would cause the two creations to run one after the other. We are using an external loop so that the two commands above can run in parallel.

The loop matches on the cluster IDs we captured rather than on the LIKE pattern alone. Terminating a cluster does not remove it from the listing, so a leftover TERMINATED cluster from an earlier run would otherwise keep the loop from ever finishing.

In [7]:

1def wait_on_state(ids, state='ACTIVE') -> None:2    """Loop until every cluster in ``ids`` reports the given state."""3    import time4
5    n_tries = 206    while n_tries > 0:7        rows = %sql SHOW CLUSTERS LIKE 'fusion-cluster-%'8        ours = [x for x in rows if x.ID in ids]9        if len(ours) == len(ids) and all(x.State == state for x in ours):10            return11        time.sleep(20)12        n_tries -= 113
14    raise RuntimeError('waiting for clusters timed out')15
16
17# Wait for both of our clusters to be active18wait_on_state({cluster_1.ID, cluster_2.ID})

We can now display the information about the clusters using the SHOW CLUSTERS command. The EXTENDED clause adds the endpoint, the cloud provider, the firewall ranges and the project the cluster belongs to.

In [8]:

1%%sql2SHOW CLUSTERS LIKE 'fusion-cluster-%' ORDER BY Name EXTENDED

Suspending and resuming clusters

It is possible to suspend and resume clusters from Fusion SQL as well.

SUSPEND CLUSTER { ID '<cluster-id>' | '<cluster-name>' } [ WAIT ON SUSPENDED ];

RESUME CLUSTER { ID '<cluster-id>' | '<cluster-name>' } [ DISABLE AUTO SUSPEND ] [ WAIT ON RESUMED ];

In [9]:

1%%sql2SUSPEND CLUSTER 'fusion-cluster-1'

The cluster should have a state of 'SUSPENDED' shortly after running the above command.

In [10]:

1%%sql2SHOW CLUSTERS LIKE 'fusion-cluster-%'

To resume the cluster, you use the RESUME CLUSTER command.

In [11]:

1%%sql2RESUME CLUSTER 'fusion-cluster-1' WAIT ON RESUMED

Display the information about the clusters again.

In [12]:

1cluster_info = %sql SHOW CLUSTERS LIKE 'fusion-cluster-%' EXTENDED2cluster_info

Accessing the database endpoint of a cluster

As you saw above, we have access to the database endpoint in the cluster information. Together with the admin password that CREATE CLUSTER reported, we can use that to create a connection to the cluster for database operations.

The connection parameters are passed as keyword arguments rather than interpolated into a connection string. The generated admin password is not URL-safe — it can contain ?, /, @, #, : and % — and any of those change how a URL is read: a ? starts the query string, so everything after it drops out of the host, and a % starts an escape sequence. Keyword arguments are taken literally, so the password arrives as issued.

In [13]:

1import singlestoredb as s22
3endpoint = {x.Name: x.Endpoint for x in cluster_info}['fusion-cluster-1']4password = cluster_1.AdminPassword5
6with s2.connect(7    host=endpoint, port=3306, user='admin', password=password,8) as conn:9    with conn.cursor() as cur:10        cur.execute('show databases')11        for row in cur:12            print(*row)

Terminating clusters

You can terminate clusters from Fusion SQL commands as well.

DROP CLUSTER [ IF EXISTS ] { ID '<cluster-id>' | '<cluster-name>' } [ WAIT ON TERMINATED ];

Dropping the cluster is the whole of the cleanup. All databases attached to the cluster are detached when it is deleted, and nothing is left behind to remove afterwards.

Let's drop fusion-cluster-2 and leave fusion-cluster-1 in place.

In [14]:

1%%sql2DROP CLUSTER 'fusion-cluster-2'

The above operation may take a few seconds. Once it has completed, fusion-cluster-2 will report a state of TERMINATED. Note that a terminated cluster is not removed from the listing, so it keeps showing up in SHOW CLUSTERS with that state.

In [15]:

1%%sql2SHOW CLUSTERS LIKE 'fusion-cluster-%'

Now let's remove the remaining cluster, this time waiting for the termination to finish before continuing.

In [16]:

1%%sql2DROP CLUSTER 'fusion-cluster-1' WAIT ON TERMINATED

In [17]:

1%%sql2SHOW CLUSTERS LIKE 'fusion-cluster-%'

Naming a cluster that does not exist raises a KeyError. Add the IF EXISTS clause when you would rather the command do nothing.

In [18]:

1try:2    %sql DROP CLUSTER 'no-such-cluster'3except KeyError:4    print('no cluster was found')5
6%sql DROP CLUSTER IF EXISTS 'no-such-cluster'

Conclusion

We have covered the Fusion SQL commands for creating and terminating clusters, and we demonstrated how to suspend and resume one. Fusion SQL can also manage your Stage files. That topic is covered in another example notebook, and more Fusion SQL commands will be added as features are added to SingleStore Cloud.

Details


About this Template

Fusion SQL allows you to manage your SingleStore Cloud resources such as clusters and Stage files all from SQL.

This Notebook can be run in Standard and Enterprise deployments.

Tags

starterfusionpython

See Notebook in action

Launch this notebook in SingleStore and start executing queries instantly.

License

This Notebook has been released under the Apache 2.0 open source license.