Passed
Push — master ( f29192...6668ab )
by Tolga
01:03 queued 14s
created

utils.IsContextRelatedError   A

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
package utils
2
3
import (
4
	"context"
5
	"fmt"
6
	"log/slog"
7
8
	"go.opentelemetry.io/otel/codes"
9
	"go.opentelemetry.io/otel/trace"
10
11
	"github.com/pkg/errors"
12
13
	"github.com/Masterminds/squirrel"
14
15
	base "github.com/Permify/permify/pkg/pb/base/v1"
16
)
17
18
const (
19
	BulkEntityFilterTemplate = `
20
    WITH entities AS (
21
        (SELECT id, entity_id, entity_type, tenant_id, created_tx_id, expired_tx_id FROM relation_tuples)
22
        UNION ALL
23
        (SELECT id, entity_id, entity_type, tenant_id, created_tx_id, expired_tx_id FROM attributes)
24
    ), filtered_entities AS (
25
        SELECT DISTINCT ON (entity_id) id, entity_id
26
        FROM entities
27
        WHERE tenant_id = '%s'
28
        AND entity_type = '%s'
29
        AND %s
30
        AND %s
31
    )
32
    SELECT id, entity_id
33
    FROM filtered_entities`
34
)
35
36
// SnapshotQuery adds conditions to a SELECT query for checking transaction visibility based on created and expired transaction IDs.
37
// The query checks if transactions are visible in a snapshot associated with the provided value.
38
func SnapshotQuery(sl squirrel.SelectBuilder, value uint64) squirrel.SelectBuilder {
39
	// Convert the value to a string once to reduce redundant calls to fmt.Sprintf.
40
	valStr := fmt.Sprintf("'%v'::xid8", value)
41
42
	// Create a subquery for the snapshot associated with the provided value.
43
	snapshotQuery := fmt.Sprintf("(select snapshot from transactions where id = %s)", valStr)
44
45
	// Create an expression to check if a transaction with a specific created_tx_id is visible in the snapshot.
46
	visibilityExpr := squirrel.Expr(fmt.Sprintf("pg_visible_in_snapshot(created_tx_id, %s) = true", snapshotQuery))
47
	// Create an expression to check if the created_tx_id is equal to the provided value.
48
	createdExpr := squirrel.Expr(fmt.Sprintf("created_tx_id = %s", valStr))
49
	// Use OR condition for the created expressions.
50
	createdWhere := squirrel.Or{visibilityExpr, createdExpr}
51
52
	// Create an expression to check if a transaction with a specific expired_tx_id is not visible in the snapshot.
53
	expiredVisibilityExpr := squirrel.Expr(fmt.Sprintf("pg_visible_in_snapshot(expired_tx_id, %s) = false", snapshotQuery))
54
	// Create an expression to check if the expired_tx_id is equal to zero.
55
	expiredZeroExpr := squirrel.Expr("expired_tx_id = '0'::xid8")
56
	// Create an expression to check if the expired_tx_id is not equal to the provided value.
57
	expiredNotExpr := squirrel.Expr(fmt.Sprintf("expired_tx_id <> %s", valStr))
58
	// Use AND condition for the expired expressions, checking both visibility and non-equality with value.
59
	expiredWhere := squirrel.And{squirrel.Or{expiredVisibilityExpr, expiredZeroExpr}, expiredNotExpr}
60
61
	// Add the created and expired conditions to the SELECT query.
62
	return sl.Where(createdWhere).Where(expiredWhere)
63
}
64
65
// snapshotQuery function generates two strings representing conditions to be applied in a SQL query to filter data based on visibility of transactions.
66
func snapshotQuery(value uint64) (string, string) {
67
	// Convert the provided value into a string format suitable for our SQL query, formatted as a transaction ID.
68
	valStr := fmt.Sprintf("'%v'::xid8", value)
69
70
	// Create a subquery that fetches the snapshot associated with the transaction ID.
71
	snapshotQ := fmt.Sprintf("(SELECT snapshot FROM transactions WHERE id = %s)", valStr)
72
73
	// Create an expression that checks whether a transaction (represented by 'created_tx_id') is visible in the snapshot.
74
	visibilityExpr := fmt.Sprintf("pg_visible_in_snapshot(created_tx_id, %s) = true", snapshotQ)
75
	// Create an expression that checks if the 'created_tx_id' is the same as our transaction ID.
76
	createdExpr := fmt.Sprintf("created_tx_id = %s", valStr)
77
	// Combine these expressions to form a condition. A row will satisfy this condition if either of the expressions are true.
78
	createdWhere := fmt.Sprintf("(%s OR %s)", visibilityExpr, createdExpr)
79
80
	// Create an expression that checks whether a transaction (represented by 'expired_tx_id') is not visible in the snapshot.
81
	expiredVisibilityExpr := fmt.Sprintf("pg_visible_in_snapshot(expired_tx_id, %s) = false", snapshotQ)
82
	// Create an expression that checks if the 'expired_tx_id' is zero. This handles cases where the transaction hasn't expired.
83
	expiredZeroExpr := "expired_tx_id = '0'::xid8"
84
	// Create an expression that checks if the 'expired_tx_id' is not the same as our transaction ID.
85
	expiredNotExpr := fmt.Sprintf("expired_tx_id <> %s", valStr)
86
	// Combine these expressions to form a condition. A row will satisfy this condition if the first set of expressions are true (either the transaction hasn't expired, or if it has, it's not visible in the snapshot) and the second expression is also true (the 'expired_tx_id' is not the same as our transaction ID).
87
	expiredWhere := fmt.Sprintf("(%s AND %s)", fmt.Sprintf("(%s OR %s)", expiredVisibilityExpr, expiredZeroExpr), expiredNotExpr)
88
89
	// Return the conditions for both 'created' and 'expired' transactions. These can be used in a WHERE clause of a SQL query to filter results.
90
	return createdWhere, expiredWhere
91
}
92
93
// BulkEntityFilterQuery -
94
func BulkEntityFilterQuery(tenantID, entityType string, snap uint64) string {
95
	createdWhere, expiredWhere := snapshotQuery(snap)
96
	return fmt.Sprintf(BulkEntityFilterTemplate, tenantID, entityType, createdWhere, expiredWhere)
97
}
98
99
// GenerateGCQuery generates a Squirrel DELETE query builder for garbage collection.
100
// It constructs a query to delete expired records from the specified table
101
// based on the provided value, which represents a transaction ID.
102
func GenerateGCQuery(table string, value uint64) squirrel.DeleteBuilder {
103
	// Convert the provided value into a string format suitable for our SQL query, formatted as a transaction ID.
104
	valStr := fmt.Sprintf("'%v'::xid8", value)
105
106
	// Create a Squirrel DELETE builder for the specified table.
107
	deleteBuilder := squirrel.Delete(table)
108
109
	// Create an expression to check if 'expired_tx_id' is not equal to '0' (not expired).
110
	expiredZeroExpr := squirrel.Expr("expired_tx_id <> '0'::xid8")
111
112
	// Create an expression to check if 'expired_tx_id' is less than the provided value (before the cutoff).
113
	beforeExpr := squirrel.Expr(fmt.Sprintf("expired_tx_id < %s", valStr))
114
115
	// Add the WHERE clauses to the DELETE query builder to filter and delete expired data.
116
	return deleteBuilder.Where(expiredZeroExpr).Where(beforeExpr)
117
}
118
119
// HandleError records an error in the given span, logs the error, and returns a standardized error.
120
// This function is used for consistent error handling across different parts of the application.
121
func HandleError(span trace.Span, err error, errorCode base.ErrorCode) error {
122
	// Record the error on the span
123
	span.RecordError(err)
124
125
	// Set the status of the span
126
	span.SetStatus(codes.Error, err.Error())
127
128
	// Check if the error is context-related
129
	if IsContextRelatedError(err) {
130
		// Use debug level logging for context-related errors
131
		slog.Debug("Context-related error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode))
132
	} else {
133
		// Use error level logging for all other errors
134
		slog.Error("Error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode))
135
	}
136
137
	// Return a new standardized error with the provided error code
138
	return errors.New(errorCode.String())
139
}
140
141
// IsContextRelatedError checks if the error is due to context cancellation, deadline exceedance, or closed connection
142
func IsContextRelatedError(err error) bool {
143
	return errors.Is(err, context.Canceled) ||
144
		errors.Is(err, context.DeadlineExceeded) ||
145
		err.Error() == "conn closed"
146
}
147