/
etcdkey.go
170 lines (157 loc) · 4.62 KB
/
etcdkey.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
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package etcd
import (
"strings"
"github.com/pingcap/log"
cerror "github.com/tikv/migration/cdc/pkg/errors"
)
const (
// EtcdKeyBase is the common prefix of the keys in CDC
EtcdKeyBase = "/tikv/cdc"
ownerKey = "/owner"
captureKey = "/capture"
taskKey = "/task"
taskWorkloadKey = taskKey + "/workload"
taskStatusKey = taskKey + "/status"
taskPositionKey = taskKey + "/position"
changefeedInfoKey = "/changefeed/info"
jobKey = "/job"
)
// CDCKeyType is the type of etcd key
type CDCKeyType = int
// the types of etcd key
const (
CDCKeyTypeUnknown CDCKeyType = iota
CDCKeyTypeOwner
CDCKeyTypeCapture
CDCKeyTypeChangefeedInfo
CDCKeyTypeChangeFeedStatus
CDCKeyTypeTaskPosition
CDCKeyTypeTaskStatus
CDCKeyTypeTaskWorkload
)
// CDCKey represents a etcd key which is defined by TiCDC
/*
Usage:
we can parse a raw etcd key:
```
k := new(CDCKey)
rawKey := "/tikv/cdc/changefeed/info/test/changefeed"
err := k.Parse(rawKey)
c.Assert(k, check.DeepEquals, &CDCKey{
Tp: CDCKeyTypeChangefeedInfo,
ChangefeedID: "test/changefeed",
})
```
and we can generate a raw key from CDCKey
```
k := &CDCKey{
Tp: CDCKeyTypeChangefeedInfo,
ChangefeedID: "test/changefeed",
}
c.Assert(k.String(), check.Equals, "/tikv/cdc/changefeed/info/test/changefeed")
```
*/
type CDCKey struct {
Tp CDCKeyType
ChangefeedID string
CaptureID string
OwnerLeaseID string
}
// Parse parses the given etcd key
func (k *CDCKey) Parse(key string) error {
if !strings.HasPrefix(key, EtcdKeyBase) {
return cerror.ErrInvalidEtcdKey.GenWithStackByArgs(key)
}
key = key[len(EtcdKeyBase):]
switch {
case strings.HasPrefix(key, ownerKey):
k.Tp = CDCKeyTypeOwner
k.CaptureID = ""
k.ChangefeedID = ""
key = key[len(ownerKey):]
if len(key) > 0 {
key = key[1:]
}
k.OwnerLeaseID = key
case strings.HasPrefix(key, captureKey):
k.Tp = CDCKeyTypeCapture
k.CaptureID = key[len(captureKey)+1:]
k.ChangefeedID = ""
k.OwnerLeaseID = ""
case strings.HasPrefix(key, changefeedInfoKey):
k.Tp = CDCKeyTypeChangefeedInfo
k.CaptureID = ""
k.ChangefeedID = key[len(changefeedInfoKey)+1:]
k.OwnerLeaseID = ""
case strings.HasPrefix(key, jobKey):
k.Tp = CDCKeyTypeChangeFeedStatus
k.CaptureID = ""
k.ChangefeedID = key[len(jobKey)+1:]
k.OwnerLeaseID = ""
case strings.HasPrefix(key, taskStatusKey):
splitKey := strings.SplitN(key[len(taskStatusKey)+1:], "/", 2)
if len(splitKey) != 2 {
return cerror.ErrInvalidEtcdKey.GenWithStackByArgs(key)
}
k.Tp = CDCKeyTypeTaskStatus
k.CaptureID = splitKey[0]
k.ChangefeedID = splitKey[1]
k.OwnerLeaseID = ""
case strings.HasPrefix(key, taskPositionKey):
splitKey := strings.SplitN(key[len(taskPositionKey)+1:], "/", 2)
if len(splitKey) != 2 {
return cerror.ErrInvalidEtcdKey.GenWithStackByArgs(key)
}
k.Tp = CDCKeyTypeTaskPosition
k.CaptureID = splitKey[0]
k.ChangefeedID = splitKey[1]
k.OwnerLeaseID = ""
case strings.HasPrefix(key, taskWorkloadKey):
splitKey := strings.SplitN(key[len(taskWorkloadKey)+1:], "/", 2)
if len(splitKey) != 2 {
return cerror.ErrInvalidEtcdKey.GenWithStackByArgs(key)
}
k.Tp = CDCKeyTypeTaskWorkload
k.CaptureID = splitKey[0]
k.ChangefeedID = splitKey[1]
k.OwnerLeaseID = ""
default:
return cerror.ErrInvalidEtcdKey.GenWithStackByArgs(key)
}
return nil
}
func (k *CDCKey) String() string {
switch k.Tp {
case CDCKeyTypeOwner:
if len(k.OwnerLeaseID) == 0 {
return EtcdKeyBase + ownerKey
}
return EtcdKeyBase + ownerKey + "/" + k.OwnerLeaseID
case CDCKeyTypeCapture:
return EtcdKeyBase + captureKey + "/" + k.CaptureID
case CDCKeyTypeChangefeedInfo:
return EtcdKeyBase + changefeedInfoKey + "/" + k.ChangefeedID
case CDCKeyTypeChangeFeedStatus:
return EtcdKeyBase + jobKey + "/" + k.ChangefeedID
case CDCKeyTypeTaskPosition:
return EtcdKeyBase + taskPositionKey + "/" + k.CaptureID + "/" + k.ChangefeedID
case CDCKeyTypeTaskStatus:
return EtcdKeyBase + taskStatusKey + "/" + k.CaptureID + "/" + k.ChangefeedID
case CDCKeyTypeTaskWorkload:
return EtcdKeyBase + taskWorkloadKey + "/" + k.CaptureID + "/" + k.ChangefeedID
}
log.Panic("unreachable")
return ""
}