-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
metadata.go
159 lines (132 loc) · 5.49 KB
/
metadata.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
/*
Copyright 2023 The Dapr Authors
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 reminders
import (
"hash/fnv"
"strconv"
"github.com/google/uuid"
"github.com/dapr/dapr/pkg/actors/internal"
)
// ActorMetadata represents information about the actor type.
type ActorMetadata struct {
ID string `json:"id"`
RemindersMetadata ActorRemindersMetadata `json:"actorRemindersMetadata"`
Etag *string `json:"-"`
}
// ActorRemindersMetadata represents information about actor's reminders.
type ActorRemindersMetadata struct {
PartitionCount int `json:"partitionCount"`
PartitionsEtag map[uint32]*string `json:"-"`
}
type ActorReminderReference struct {
ActorMetadataID string
ActorRemindersPartitionID uint32
Reminder internal.Reminder
}
func (m *ActorMetadata) calculateReminderPartition(actorID, reminderName string) uint32 {
if m.RemindersMetadata.PartitionCount <= 0 {
return 0
}
// do not change this hash function because it would be a breaking change.
h := fnv.New32a()
h.Write([]byte(actorID))
h.Write([]byte(reminderName))
return (h.Sum32() % uint32(m.RemindersMetadata.PartitionCount)) + 1
}
func (m *ActorMetadata) createReminderReference(reminder internal.Reminder) ActorReminderReference {
if m.RemindersMetadata.PartitionCount > 0 {
return ActorReminderReference{
ActorMetadataID: m.ID,
ActorRemindersPartitionID: m.calculateReminderPartition(reminder.ActorID, reminder.Name),
Reminder: reminder,
}
}
return ActorReminderReference{
ActorMetadataID: uuid.Nil.String(),
ActorRemindersPartitionID: 0,
Reminder: reminder,
}
}
func (m *ActorMetadata) calculateRemindersStateKey(actorType string, remindersPartitionID uint32) string {
if remindersPartitionID == 0 {
return constructCompositeKey("actors", actorType)
}
return constructCompositeKey(
"actors",
actorType,
m.ID,
"reminders",
strconv.Itoa(int(remindersPartitionID)))
}
func (m *ActorMetadata) calculateEtag(partitionID uint32) *string {
return m.RemindersMetadata.PartitionsEtag[partitionID]
}
func (m *ActorMetadata) removeReminderFromPartition(reminderRefs []ActorReminderReference, actorType, actorID, reminderName string) (bool, []internal.Reminder, string, *string) {
// First, we find the partition
var partitionID uint32
l := len(reminderRefs)
if m.RemindersMetadata.PartitionCount > 0 {
var found bool
for _, reminderRef := range reminderRefs {
if reminderRef.Reminder.ActorType == actorType && reminderRef.Reminder.ActorID == actorID && reminderRef.Reminder.Name == reminderName {
partitionID = reminderRef.ActorRemindersPartitionID
found = true
break
}
}
// If the reminder doesn't exist, return without making any change
if !found {
return false, nil, "", nil
}
// When calculating the initial allocated size of remindersInPartitionAfterRemoval, if we have partitions assume len(reminderRefs)/PartitionCount for an initial count
// This is unlikely to avoid all re-allocations, but it's still better than allocating the slice with capacity 0
l /= m.RemindersMetadata.PartitionCount
}
remindersInPartitionAfterRemoval := make([]internal.Reminder, 0, l)
var found bool
for _, reminderRef := range reminderRefs {
if reminderRef.Reminder.ActorType == actorType && reminderRef.Reminder.ActorID == actorID && reminderRef.Reminder.Name == reminderName {
found = true
continue
}
// Only the items in the partition to be updated.
if reminderRef.ActorRemindersPartitionID == partitionID {
remindersInPartitionAfterRemoval = append(remindersInPartitionAfterRemoval, reminderRef.Reminder)
}
}
// If no reminder found, return false here to short-circuit the next operations
if !found {
return false, nil, "", nil
}
stateKey := m.calculateRemindersStateKey(actorType, partitionID)
return true, remindersInPartitionAfterRemoval, stateKey, m.calculateEtag(partitionID)
}
func (m *ActorMetadata) insertReminderInPartition(reminderRefs []ActorReminderReference, reminder internal.Reminder) ([]internal.Reminder, ActorReminderReference, string, *string) {
newReminderRef := m.createReminderReference(reminder)
var remindersInPartitionAfterInsertion []internal.Reminder
for _, reminderRef := range reminderRefs {
// Only the items in the partition to be updated.
if reminderRef.ActorRemindersPartitionID == newReminderRef.ActorRemindersPartitionID {
remindersInPartitionAfterInsertion = append(remindersInPartitionAfterInsertion, reminderRef.Reminder)
}
}
remindersInPartitionAfterInsertion = append(remindersInPartitionAfterInsertion, reminder)
stateKey := m.calculateRemindersStateKey(newReminderRef.Reminder.ActorType, newReminderRef.ActorRemindersPartitionID)
return remindersInPartitionAfterInsertion, newReminderRef, stateKey, m.calculateEtag(newReminderRef.ActorRemindersPartitionID)
}
func (m *ActorMetadata) calculateDatabasePartitionKey(stateKey string) string {
if m.RemindersMetadata.PartitionCount > 0 {
return m.ID
}
return stateKey
}