Skip to content

Commit

Permalink
SNOW-1621763 update example for na + spcs upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Yong Xu committed Aug 16, 2024
1 parent c7c986c commit 384b058
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 45 deletions.
18 changes: 18 additions & 0 deletions spcs-three-tier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ See the [documentation](https://other-docs.snowflake.com/en/native-apps/provider

8. Click "Publish".


#### Upgrade
In the provider account

alter application package spcs_app_pkg add version v1 using @spcs_app_pkg.napp.app_stage;
alter application package spcs_app_pkg set default release directive version=v1 patch=0;

add patch
alter application package spcs_app_pkg add patch for version v1 using @spcs_app_pkg.napp.app_stage;
alter application package spcs_app_pkg set default release directive version=v1 patch=1;

add version v2
alter application package spcs_app_pkg add version v2 using @spcs_app_pkg.napp.app_stage;
alter application package spcs_app_pkg set default release directive version=v2 patch=1;

Check the status:
If you have multiple

### Debugging
There are some Stored Procedures to allow the Consumer to see the status
and logs for the containers and services. These procedures are granted to the `app_admin`
Expand Down
12 changes: 6 additions & 6 deletions spcs-three-tier/app/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
manifest_version: 1
version:
name: V1
name: versioned_schema
label: "First Version"

configuration:
grant_callback: v1.create_services
grant_callback: versioned_schema.create_services

artifacts:
setup_script: setup.sql
Expand All @@ -20,7 +20,7 @@ artifacts:
- /spcs_app/napp/img_repo/eap_router

lifecycle_callbacks:
version_initializer: v1.init
version_initializer: versioned_schema.init

privileges:
- BIND SERVICE ENDPOINT:
Expand All @@ -39,13 +39,13 @@ references:
- SELECT
object_type: VIEW
multi_valued: false
register_callback: v1.register_single_callback
register_callback: versioned_schema.register_single_callback

- WIKIPEDIA_EAI:
label: "Wikipedia Access Integration"
description: "EAI for Egress from NA+SPCS"
privileges: [USAGE]
object_type: EXTERNAL_ACCESS_INTEGRATION
register_callback: v1.register_single_callback
configuration_callback: v1.get_configuration
register_callback: versioned_schema.register_single_callback
configuration_callback: versioned_schema.get_configuration
required_at_setup: true
63 changes: 37 additions & 26 deletions spcs-three-tier/app/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ CREATE APPLICATION ROLE IF NOT EXISTS app_user;
CREATE SCHEMA IF NOT EXISTS app_public;
GRANT USAGE ON SCHEMA app_public TO APPLICATION ROLE app_admin;
GRANT USAGE ON SCHEMA app_public TO APPLICATION ROLE app_user;
CREATE OR ALTER VERSIONED SCHEMA v1;
GRANT USAGE ON SCHEMA v1 TO APPLICATION ROLE app_admin;
CREATE OR ALTER VERSIONED SCHEMA versioned_schema;
GRANT USAGE ON SCHEMA versioned_schema TO APPLICATION ROLE app_admin;


CREATE OR REPLACE PROCEDURE v1.register_single_callback(ref_name STRING, operation STRING, ref_or_alias STRING)
CREATE OR REPLACE PROCEDURE versioned_schema.register_single_callback(ref_name STRING, operation STRING, ref_or_alias STRING)
RETURNS STRING
LANGUAGE SQL
AS $$
Expand All @@ -25,9 +25,9 @@ CREATE OR REPLACE PROCEDURE v1.register_single_callback(ref_name STRING, operati
RETURN 'Operation ' || operation || ' succeeds.';
END;
$$;
GRANT USAGE ON PROCEDURE v1.register_single_callback(STRING, STRING, STRING) TO APPLICATION ROLE app_admin;
GRANT USAGE ON PROCEDURE versioned_schema.register_single_callback(STRING, STRING, STRING) TO APPLICATION ROLE app_admin;

CREATE OR REPLACE PROCEDURE v1.get_configuration(ref_name STRING)
CREATE OR REPLACE PROCEDURE versioned_schema.get_configuration(ref_name STRING)
RETURNS STRING
LANGUAGE SQL
AS
Expand All @@ -47,26 +47,33 @@ BEGIN
END;
$$;

GRANT USAGE ON PROCEDURE v1.get_configuration(STRING) TO APPLICATION ROLE app_admin;
GRANT USAGE ON PROCEDURE versioned_schema.get_configuration(STRING) TO APPLICATION ROLE app_admin;

-- The version initializer callback is executed after a successful installation, upgrade, or downgrade of an application object.
-- In case the application fails to upgrade, the version initializer of the previous (successful) version will be executed so you
-- In case the application fails to upgrade, the version initializer of the previous (successful) version will be executed so you
-- can clean up application state that may have been modified during the failed upgrade.
CREATE OR REPLACE PROCEDURE v1.init()
RETURNS STRING

-- this is the first the version(version v1 patch 0) of the app package. We consider the case that when the
-- app is upgraded to next version and try to alter the services and it fails. In that case
-- it will fail back to version v1 patch 0 and call this procedure versioned_schema.init() to
-- restore the services and app can fully function.
CREATE OR REPLACE PROCEDURE versioned_schema.init()
RETURNS STRING
LANGUAGE SQL
EXECUTE AS OWNER
EXECUTE AS OWNER
AS
$$
BEGIN
ALTER SERVICE IF EXISTS app_public.frontend FROM SPECIFICATION_FILE='frontend.yaml';
ALTER SERVICE IF EXISTS app_public.backend FROM SPECIFICATION_FILE='backend.yaml';
BEGIN
--create services if not exist
call versioned_schema.create_services();
--wait for 300 seconds, but it will stop if two services have READY status,
-- or any of the services has the FAILED status.
select system$wait_for_services(300, 'app_public.backend', 'app_public.frontend');

RETURN 'init complete';
END $$;

GRANT USAGE ON PROCEDURE v1.init() TO APPLICATION ROLE app_admin;

CREATE OR REPLACE PROCEDURE v1.start_backend(pool_name VARCHAR)
CREATE OR REPLACE PROCEDURE versioned_schema.start_backend(pool_name VARCHAR)
RETURNS string
LANGUAGE sql
AS $$
Expand All @@ -78,9 +85,9 @@ BEGIN
GRANT USAGE ON SERVICE app_public.backend TO APPLICATION ROLE app_user;
END
$$;
GRANT USAGE ON PROCEDURE v1.start_backend(VARCHAR) TO APPLICATION ROLE app_admin;
GRANT USAGE ON PROCEDURE versioned_schema.start_backend(VARCHAR) TO APPLICATION ROLE app_admin;

CREATE OR REPLACE PROCEDURE v1.start_frontend(pool_name VARCHAR)
CREATE OR REPLACE PROCEDURE versioned_schema.start_frontend(pool_name VARCHAR)
RETURNS string
LANGUAGE sql
AS $$
Expand All @@ -95,10 +102,10 @@ BEGIN
RETURN 'Service started. Check status, and when ready, get URL';
END
$$;
GRANT USAGE ON PROCEDURE v1.start_frontend(VARCHAR) TO APPLICATION ROLE app_admin;
GRANT USAGE ON PROCEDURE versioned_schema.start_frontend(VARCHAR) TO APPLICATION ROLE app_admin;


CREATE OR REPLACE PROCEDURE v1.create_services(privileges array)
CREATE OR REPLACE PROCEDURE versioned_schema.create_services()
RETURNS STRING
LANGUAGE SQL
AS
Expand All @@ -114,11 +121,15 @@ CREATE OR REPLACE PROCEDURE v1.create_services(privileges array)
MAX_NODES = 1
INSTANCE_FAMILY = CPU_X64_XS;

CALL v1.start_backend('backend_compute_pool');
CALL v1.start_frontend('frontend_compute_pool');
CALL versioned_schema.start_backend('backend_compute_pool');
CALL versioned_schema.start_frontend('frontend_compute_pool');

# needed for installation from listing/cross account
GRANT SERVICE ROLE app_public.frontend!ALL_ENDPOINTS_USAGE TO APPLICATION ROLE app_admin;

GRANT USAGE ON PROCEDURE versioned_schema.create_services( ) TO APPLICATION ROLE app_admin;
END;
$$;
GRANT USAGE ON PROCEDURE v1.create_services(array) TO APPLICATION ROLE app_admin;


CREATE OR REPLACE PROCEDURE app_public.stop_app()
Expand All @@ -133,7 +144,7 @@ END
$$;
GRANT USAGE ON PROCEDURE app_public.stop_app() TO APPLICATION ROLE app_admin;

CREATE OR REPLACE PROCEDURE v1.app_url()
CREATE OR REPLACE PROCEDURE versioned_schema.app_url()
RETURNS string
LANGUAGE sql
AS
Expand All @@ -146,8 +157,8 @@ BEGIN
RETURN ingress_url;
END
$$;
GRANT USAGE ON PROCEDURE v1.app_url() TO APPLICATION ROLE app_admin;
GRANT USAGE ON PROCEDURE v1.app_url() TO APPLICATION ROLE app_user;
GRANT USAGE ON PROCEDURE versioned_schema.app_url() TO APPLICATION ROLE app_admin;
GRANT USAGE ON PROCEDURE versioned_schema.app_url() TO APPLICATION ROLE app_user;

-- Support functions
EXECUTE IMMEDIATE FROM 'support.sql';
48 changes: 48 additions & 0 deletions spcs-three-tier/app_for_upgrade/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
manifest_version: 2
version:
name: versioned_schema
label: "Second Version"

artifacts:
setup_script: setup.sql

default_web_endpoint:
service: app_public.frontend
endpoint: app

container_services:
images:
- /spcs_app/napp/img_repo/eap_frontend
- /spcs_app/napp/img_repo/eap_backend
- /spcs_app/napp/img_repo/eap_router

lifecycle_callbacks:
version_initializer: versioned_schema.init

privileges:
- BIND SERVICE ENDPOINT:
description: "Ability to create ingress URLs."
required_at_setup: true
- CREATE COMPUTE POOL:
required_at_setup: true
description: "Enable appplication to create its own compute pool(s)"


references:
- ORDERS_TABLE:
label: "Orders table"
description: "Orders table in TPC-H samples"
privileges:
- SELECT
object_type: VIEW
multi_valued: false
register_callback: versioned_schema.register_single_callback

- WIKIPEDIA_EAI:
label: "Wikipedia Access Integration"
description: "EAI for Egress from NA+SPCS"
privileges: [USAGE]
object_type: EXTERNAL_ACCESS_INTEGRATION
register_callback: versioned_schema.register_single_callback
configuration_callback: versioned_schema.get_configuration
required_at_setup: true
Loading

0 comments on commit 384b058

Please sign in to comment.