This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
/
setup.go
218 lines (194 loc) · 6.38 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package setup
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"golang.org/x/sync/errgroup"
)
const timeToLiveAttributeName = "ExpireTime"
func recreateTable(ctx context.Context, svc *dynamodb.Client, input *dynamodb.CreateTableInput) error {
_, err := svc.DeleteTable(ctx, &dynamodb.DeleteTableInput{
TableName: input.TableName,
})
if err == nil {
wDelete := dynamodb.NewTableNotExistsWaiter(svc)
if err := wDelete.Wait(ctx, &dynamodb.DescribeTableInput{TableName: input.TableName}, time.Minute*5); err != nil {
return fmt.Errorf("failed waiting for table deletion, %v", err)
}
} else {
var rnfe *types.ResourceNotFoundException
if !errors.As(err, &rnfe) {
return fmt.Errorf("failed to delete table, %v", err)
}
}
_, err = svc.CreateTable(ctx, input)
if err != nil {
return fmt.Errorf("failed to create table, %v", err)
}
wCreate := dynamodb.NewTableExistsWaiter(svc)
if err := wCreate.Wait(ctx, &dynamodb.DescribeTableInput{TableName: input.TableName}, time.Minute*5); err != nil {
return fmt.Errorf("failed waiting for table creation, %v", err)
}
_, err = svc.UpdateTimeToLive(ctx, &dynamodb.UpdateTimeToLiveInput{
TableName: input.TableName,
TimeToLiveSpecification: &types.TimeToLiveSpecification{
AttributeName: aws.String(timeToLiveAttributeName),
Enabled: aws.Bool(true),
},
})
if err != nil {
return fmt.Errorf("failed updating table ttl, %v", err)
}
return nil
}
func ensureSpansTable(ctx context.Context, svc *dynamodb.Client, tableName string) error {
var (
traceIDKey = "TraceID"
spanIDKey = "SpanID"
)
return recreateTable(ctx, svc, &dynamodb.CreateTableInput{
AttributeDefinitions: []types.AttributeDefinition{
{AttributeName: &traceIDKey, AttributeType: types.ScalarAttributeTypeS},
{AttributeName: &spanIDKey, AttributeType: types.ScalarAttributeTypeS},
{AttributeName: aws.String("ServiceNameBucket"), AttributeType: types.ScalarAttributeTypeS},
{AttributeName: aws.String("StartTime"), AttributeType: types.ScalarAttributeTypeN},
},
BillingMode: types.BillingModePayPerRequest,
TableName: &tableName,
KeySchema: []types.KeySchemaElement{
{AttributeName: &traceIDKey, KeyType: types.KeyTypeHash},
{AttributeName: &spanIDKey, KeyType: types.KeyTypeRange},
},
GlobalSecondaryIndexes: []types.GlobalSecondaryIndex{
{
IndexName: aws.String("SpanSearchIndex"),
KeySchema: []types.KeySchemaElement{
{
AttributeName: aws.String("ServiceNameBucket"),
KeyType: types.KeyTypeHash,
},
{
AttributeName: aws.String("StartTime"),
KeyType: types.KeyTypeRange,
},
},
Projection: &types.Projection{
ProjectionType: types.ProjectionTypeInclude,
NonKeyAttributes: []string{"OperationName", "Duration", "SearchableTags"},
},
},
},
})
}
func ensureServicesTable(ctx context.Context, svc *dynamodb.Client, tableName string) error {
var (
serviceIDKey = "Name"
)
return recreateTable(ctx, svc, &dynamodb.CreateTableInput{
AttributeDefinitions: []types.AttributeDefinition{
{AttributeName: &serviceIDKey, AttributeType: types.ScalarAttributeTypeS},
},
BillingMode: types.BillingModePayPerRequest,
TableName: &tableName,
KeySchema: []types.KeySchemaElement{
{AttributeName: &serviceIDKey, KeyType: types.KeyTypeHash},
},
})
}
func ensureOperationsTable(ctx context.Context, svc *dynamodb.Client, tableName string) error {
var (
operationIDKey = "ServiceName"
operationRangeKey = "Name"
)
return recreateTable(ctx, svc, &dynamodb.CreateTableInput{
AttributeDefinitions: []types.AttributeDefinition{
{AttributeName: &operationIDKey, AttributeType: types.ScalarAttributeTypeS},
{AttributeName: &operationRangeKey, AttributeType: types.ScalarAttributeTypeS},
},
BillingMode: types.BillingModePayPerRequest,
TableName: &tableName,
KeySchema: []types.KeySchemaElement{
{AttributeName: &operationIDKey, KeyType: types.KeyTypeHash},
{AttributeName: &operationRangeKey, KeyType: types.KeyTypeRange},
},
})
}
func ensureDependenciesTable(ctx context.Context, svc *dynamodb.Client, tableName string) error {
var (
operationIDKey = "Key"
operationRangeKey = "CallTimeBucket"
)
return recreateTable(ctx, svc, &dynamodb.CreateTableInput{
AttributeDefinitions: []types.AttributeDefinition{
{AttributeName: &operationIDKey, AttributeType: types.ScalarAttributeTypeS},
{AttributeName: &operationRangeKey, AttributeType: types.ScalarAttributeTypeN},
},
BillingMode: types.BillingModePayPerRequest,
TableName: &tableName,
KeySchema: []types.KeySchemaElement{
{AttributeName: &operationIDKey, KeyType: types.KeyTypeHash},
{AttributeName: &operationRangeKey, KeyType: types.KeyTypeRange},
},
})
}
type SetupSpanOptions struct {
SpansTable string
ServicesTable string
OperationsTable string
}
func PollUntilReady(ctx context.Context, svc *dynamodb.Client) error {
var err error
for i := 0; i < 30; i++ {
_, err = svc.ListTables(ctx, &dynamodb.ListTablesInput{})
if err == nil {
break
}
time.Sleep(2 * time.Second)
}
return err
}
func RecreateSpanStoreTables(ctx context.Context, svc *dynamodb.Client, options *SetupSpanOptions) error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
if err := ensureSpansTable(ctx, svc, options.SpansTable); err != nil {
return fmt.Errorf("failed to ensure spans table, %v", err)
}
return nil
})
g.Go(func() error {
if err := ensureServicesTable(ctx, svc, options.ServicesTable); err != nil {
return fmt.Errorf("failed to ensure services table, %v", err)
}
return nil
})
g.Go(func() error {
if err := ensureOperationsTable(ctx, svc, options.OperationsTable); err != nil {
return fmt.Errorf("failed to ensure operations table, %v", err)
}
return nil
})
if err := g.Wait(); err != nil {
return err
}
return nil
}
type SetupDependencyOptions struct {
DependenciesTable string
}
func RecreateDependencyStoreTables(ctx context.Context, svc *dynamodb.Client, options *SetupDependencyOptions) error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
if err := ensureDependenciesTable(ctx, svc, options.DependenciesTable); err != nil {
return fmt.Errorf("failed to ensure dependencies table, %v", err)
}
return nil
})
if err := g.Wait(); err != nil {
return err
}
return nil
}