-
Notifications
You must be signed in to change notification settings - Fork 7
/
partition.go
138 lines (121 loc) · 3.37 KB
/
partition.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
// Copyright 2019-present Open Networking Foundation.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gossip
import (
"context"
"fmt"
"github.com/atomix/atomix-api/go/atomix/primitive"
"github.com/atomix/atomix-api/go/atomix/primitive/meta"
"github.com/atomix/atomix-go-framework/pkg/atomix/cluster"
"github.com/atomix/atomix-go-framework/pkg/atomix/time"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"math/rand"
"sync"
)
// newPartitionClient creates a new proxy partition client
func newPartitionClient(c cluster.Cluster, p cluster.Partition) *PartitionClient {
return &PartitionClient{
ID: PartitionID(p.ID()),
Partition: p,
cluster: c,
}
}
// PartitionID is a partition identifier
type PartitionID int
// partitionClient is a proxy partition client
type PartitionClient struct {
cluster.Partition
cluster cluster.Cluster
ID PartitionID
conn *grpc.ClientConn
mu sync.RWMutex
}
// Connect gets the connection to the partition
func (p *PartitionClient) Connect() (*grpc.ClientConn, error) {
p.mu.RLock()
conn := p.conn
p.mu.RUnlock()
if conn != nil {
return conn, nil
}
p.mu.Lock()
defer p.mu.Unlock()
conn = p.conn
if conn != nil {
return conn, nil
}
var replica *cluster.Replica
replicas := make([]*cluster.Replica, 0, len(p.Replicas()))
for _, r := range p.Replicas() {
member, ok := p.cluster.Member()
if ok && r.NodeID == member.NodeID {
replica = r
break
}
replicas = append(replicas, r)
}
if replica == nil {
replica = replicas[rand.Intn(len(replicas))]
}
conn, err := replica.Connect(context.Background(), cluster.WithDialOption(grpc.WithInsecure()))
if err != nil {
return nil, err
}
p.conn = conn
return conn, nil
}
// Close closes the connections
func (p *PartitionClient) Close() error {
p.mu.Lock()
conn := p.conn
p.conn = nil
p.mu.Unlock()
if conn != nil {
return conn.Close()
}
return nil
}
func newPartition(client *PartitionClient, clock time.Clock, replicas int) *Partition {
return &Partition{
PartitionClient: client,
clock: clock,
replicas: replicas,
}
}
type Partition struct {
*PartitionClient
clock time.Clock
replicas int
}
func (p *Partition) addTimestamp(timestamp *meta.Timestamp) *meta.Timestamp {
var t time.Timestamp
if timestamp != nil {
t = p.clock.Update(time.NewTimestamp(*timestamp))
} else {
t = p.clock.Increment()
}
proto := p.clock.Scheme().Codec().EncodeTimestamp(t)
return &proto
}
func (p *Partition) AddRequestHeaders(ctx context.Context, headers *primitive.RequestHeaders) context.Context {
headers.Timestamp = p.addTimestamp(headers.Timestamp)
return metadata.AppendToOutgoingContext(
ctx,
"Partition-ID", fmt.Sprint(p.ID),
"Replication-Factor", fmt.Sprint(p.replicas))
}
func (p *Partition) AddResponseHeaders(headers *primitive.ResponseHeaders) {
headers.Timestamp = p.addTimestamp(headers.Timestamp)
}