From 07b99da5fa389e2bd12623b8560454883fb8ce1e Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Thu, 28 Nov 2024 22:37:03 +0530 Subject: [PATCH] Added schemaCreator and comments in workflow Signed-off-by: Alok Kumar Singh --- .github/workflows/ci-e2e-cassandra.yml | 1 + pkg/cassandra/config/config.go | 7 +++- pkg/cassandra/config/schema.go | 45 +++++++++++++++----------- pkg/cassandra/config/schema_test.go | 7 ++-- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci-e2e-cassandra.yml b/.github/workflows/ci-e2e-cassandra.yml index 4b50c269e64..d80bbed256e 100644 --- a/.github/workflows/ci-e2e-cassandra.yml +++ b/.github/workflows/ci-e2e-cassandra.yml @@ -31,6 +31,7 @@ jobs: major: 5.x schema: v004 exclude: + # Exclude v1 as create schema on fly is available for v2 only - jaeger-version: v1 skip-apply-schema: true name: ${{ matrix.version.distribution }} ${{ matrix.version.major }} ${{ matrix.jaeger-version }} diff --git a/pkg/cassandra/config/config.go b/pkg/cassandra/config/config.go index 0af10d76739..fa6d6b3b274 100644 --- a/pkg/cassandra/config/config.go +++ b/pkg/cassandra/config/config.go @@ -203,7 +203,12 @@ func (c *Configuration) newSessionPrerequisites() error { return err } - return generateSchemaIfNotPresent(session, &c.Schema) + sc := schemaCreator{ + session: session, + schema: c.Schema, + } + + return sc.createSchemaIfNotPresent() } // NewSession creates a new Cassandra session diff --git a/pkg/cassandra/config/schema.go b/pkg/cassandra/config/schema.go index ecb9f459201..34dc7df305e 100644 --- a/pkg/cassandra/config/schema.go +++ b/pkg/cassandra/config/schema.go @@ -30,17 +30,22 @@ type TemplateParams struct { DependenciesTTLInSeconds int64 } -func constructTemplateParams(cfg Schema) TemplateParams { +type schemaCreator struct { + session cassandra.Session + schema Schema +} + +func (sc *schemaCreator) constructTemplateParams() TemplateParams { return TemplateParams{ - Keyspace: cfg.Keyspace, - Replication: fmt.Sprintf("{'class': 'NetworkTopologyStrategy', 'replication_factor': '%v' }", cfg.ReplicationFactor), - CompactionWindowInMinutes: int64(cfg.CompactionWindow / time.Minute), - TraceTTLInSeconds: int64(cfg.TraceTTL / time.Second), - DependenciesTTLInSeconds: int64(cfg.DependenciesTTL / time.Second), + Keyspace: sc.schema.Keyspace, + Replication: fmt.Sprintf("{'class': 'NetworkTopologyStrategy', 'replication_factor': '%v' }", sc.schema.ReplicationFactor), + CompactionWindowInMinutes: int64(sc.schema.CompactionWindow / time.Minute), + TraceTTLInSeconds: int64(sc.schema.TraceTTL / time.Second), + DependenciesTTLInSeconds: int64(sc.schema.DependenciesTTL / time.Second), } } -func getQueryFileAsBytes(fileName string, params TemplateParams) ([]byte, error) { +func (sc *schemaCreator) getQueryFileAsBytes(fileName string, params TemplateParams) ([]byte, error) { tmpl, err := template.ParseFS(schemaFile, fileName) if err != nil { return nil, err @@ -55,7 +60,7 @@ func getQueryFileAsBytes(fileName string, params TemplateParams) ([]byte, error) return result.Bytes(), nil } -func getQueriesFromBytes(queryFile []byte) ([]string, error) { +func (sc *schemaCreator) getQueriesFromBytes(queryFile []byte) ([]string, error) { lines := bytes.Split(queryFile, []byte("\n")) var extractedLines [][]byte @@ -68,11 +73,13 @@ func getQueriesFromBytes(queryFile []byte) ([]string, error) { line = line[0:commentIndex] } - if len(line) == 0 { + trimmedLine := bytes.TrimSpace(line) + + if len(trimmedLine) == 0 { continue } - extractedLines = append(extractedLines, bytes.TrimSpace(line)) + extractedLines = append(extractedLines, trimmedLine) } var queries []string @@ -94,36 +101,36 @@ func getQueriesFromBytes(queryFile []byte) ([]string, error) { return queries, nil } -func getCassandraQueriesFromQueryStrings(session cassandra.Session, queries []string) []cassandra.Query { +func (sc *schemaCreator) getCassandraQueriesFromQueryStrings(queries []string) []cassandra.Query { var casQueries []cassandra.Query for _, query := range queries { - casQueries = append(casQueries, session.Query(query)) + casQueries = append(casQueries, sc.session.Query(query)) } return casQueries } -func contructSchemaQueries(session cassandra.Session, cfg *Schema) ([]cassandra.Query, error) { - params := constructTemplateParams(*cfg) +func (sc *schemaCreator) contructSchemaQueries() ([]cassandra.Query, error) { + params := sc.constructTemplateParams() - queryFile, err := getQueryFileAsBytes(`v004-go-tmpl.cql.tmpl`, params) + queryFile, err := sc.getQueryFileAsBytes(`v004-go-tmpl.cql.tmpl`, params) if err != nil { return nil, err } - queryStrings, err := getQueriesFromBytes(queryFile) + queryStrings, err := sc.getQueriesFromBytes(queryFile) if err != nil { return nil, err } - casQueries := getCassandraQueriesFromQueryStrings(session, queryStrings) + casQueries := sc.getCassandraQueriesFromQueryStrings(queryStrings) return casQueries, nil } -func generateSchemaIfNotPresent(session cassandra.Session, cfg *Schema) error { - casQueries, err := contructSchemaQueries(session, cfg) +func (sc *schemaCreator) createSchemaIfNotPresent() error { + casQueries, err := sc.contructSchemaQueries() if err != nil { return err } diff --git a/pkg/cassandra/config/schema_test.go b/pkg/cassandra/config/schema_test.go index 9b614a136aa..b5bebb06e5d 100644 --- a/pkg/cassandra/config/schema_test.go +++ b/pkg/cassandra/config/schema_test.go @@ -30,8 +30,9 @@ query1-finished; `, } + sc := schemaCreator{} queriesAsBytes := []byte(queriesAsString) - queries, err := getQueriesFromBytes(queriesAsBytes) + queries, err := sc.getQueriesFromBytes(queriesAsBytes) require.NoError(t, err) require.Equal(t, len(expGeneratedQueries), len(queries)) @@ -51,8 +52,8 @@ func TestInvalidQueryTemplate(t *testing.T) { query2; query-3 query-3-continue query-3-finished -- missing semicolon ` - + sc := schemaCreator{} queriesAsBytes := []byte(queriesAsString) - _, err := getQueriesFromBytes(queriesAsBytes) + _, err := sc.getQueriesFromBytes(queriesAsBytes) require.ErrorContains(t, err, "invalid template") }