* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
182 lines
5.3 KiB
Go
182 lines
5.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package cluster
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
)
|
|
|
|
// DownstreamManager associates downstream worker key identifiers with the
|
|
// connections to a specific server.
|
|
// It is safe to access DownstreamManager concurrently.
|
|
type DownstreamManager struct {
|
|
keyToWorkerId map[string]string
|
|
workerIdToKeyIds map[string]map[string]struct{}
|
|
|
|
// maps a key id to a connection initiated by a downstream worker
|
|
workerConnections map[string][]net.Conn
|
|
l sync.RWMutex
|
|
}
|
|
|
|
func NewDownstreamManager() *DownstreamManager {
|
|
return &DownstreamManager{
|
|
workerConnections: make(map[string][]net.Conn),
|
|
keyToWorkerId: make(map[string]string),
|
|
workerIdToKeyIds: make(map[string]map[string]struct{}),
|
|
}
|
|
}
|
|
|
|
func (m *DownstreamManager) mapKeyToWorkerId(keyId, wId string) {
|
|
m.l.Lock()
|
|
defer m.l.Unlock()
|
|
m.keyToWorkerId[keyId] = wId
|
|
if _, ok := m.workerIdToKeyIds[wId]; !ok {
|
|
m.workerIdToKeyIds[wId] = make(map[string]struct{})
|
|
}
|
|
m.workerIdToKeyIds[wId][keyId] = struct{}{}
|
|
}
|
|
|
|
// addConnection adds a connection associated with the provided downstream
|
|
// worker key identifier.
|
|
func (m *DownstreamManager) addConnection(id string, c net.Conn) {
|
|
m.l.Lock()
|
|
defer m.l.Unlock()
|
|
m.workerConnections[id] = append(m.workerConnections[id], c)
|
|
}
|
|
|
|
// disconnectWorkerId closes all connections associated with the provided
|
|
// worker public id
|
|
func (m *DownstreamManager) disconnectWorkerId(id string) {
|
|
m.l.Lock()
|
|
defer m.l.Unlock()
|
|
for k := range m.workerIdToKeyIds[id] {
|
|
for _, c := range m.workerConnections[k] {
|
|
c.Close()
|
|
}
|
|
delete(m.workerConnections, k)
|
|
delete(m.keyToWorkerId, k)
|
|
}
|
|
delete(m.workerIdToKeyIds, id)
|
|
}
|
|
|
|
// disconnectKeys closes all connections associated with the provided worker key
|
|
// identifier.
|
|
func (m *DownstreamManager) disconnectKeys(id string) {
|
|
m.l.Lock()
|
|
defer m.l.Unlock()
|
|
for _, c := range m.workerConnections[id] {
|
|
c.Close()
|
|
}
|
|
delete(m.workerConnections, id)
|
|
|
|
// clean up all the associations between worker id and key ids
|
|
if wId, ok := m.keyToWorkerId[id]; ok {
|
|
delete(m.keyToWorkerId, id)
|
|
delete(m.workerIdToKeyIds[wId], id)
|
|
if len(m.workerIdToKeyIds[wId]) == 0 {
|
|
delete(m.workerIdToKeyIds, wId)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Connected returns a connected state which provides the worker ids that are
|
|
// being tracked and any key ids for which we don't know the worker id.
|
|
func (m *DownstreamManager) Connected() *connectedState {
|
|
m.l.RLock()
|
|
defer m.l.RUnlock()
|
|
seenWIds := make(map[string]struct{})
|
|
var workerIds, unmappedKeyIds, allKeyIds []string
|
|
for k, v := range m.workerConnections {
|
|
if len(v) > 0 {
|
|
allKeyIds = append(allKeyIds, k)
|
|
if w, ok := m.keyToWorkerId[k]; ok {
|
|
if _, ok := seenWIds[w]; !ok {
|
|
seenWIds[w] = struct{}{}
|
|
workerIds = append(workerIds, w)
|
|
}
|
|
} else {
|
|
unmappedKeyIds = append(unmappedKeyIds, k)
|
|
}
|
|
}
|
|
}
|
|
return &connectedState{
|
|
dm: m,
|
|
allKeyIds: allKeyIds,
|
|
unmappedKeyIds: unmappedKeyIds,
|
|
workerIds: workerIds,
|
|
}
|
|
}
|
|
|
|
// connectedState provides a state of the downstream manager and allows users
|
|
// to request connections be disconnected that are capturred in this state.
|
|
type connectedState struct {
|
|
dm *DownstreamManager
|
|
allKeyIds []string
|
|
unmappedKeyIds []string
|
|
workerIds []string
|
|
}
|
|
|
|
// AllKeyIds returns all key ids for downstream workers.
|
|
// Deprecated. Please use Unmapped KeyIds instead when communicating with
|
|
// controllers running version >= 0.12.0 HCP and >0.12.0 OSS.
|
|
func (s *connectedState) AllKeyIds() []string {
|
|
return s.allKeyIds
|
|
}
|
|
|
|
// UnmappedKeyIds are the key ids for which no worker id association is known
|
|
// and which are tracking at least 1 connection.
|
|
func (s *connectedState) UnmappedKeyIds() []string {
|
|
return s.unmappedKeyIds
|
|
}
|
|
|
|
// WorkerIds are the public ids for workers which we are tracking connections.
|
|
func (s *connectedState) WorkerIds() []string {
|
|
return s.workerIds
|
|
}
|
|
|
|
// DisconnectMissingWorkers disconnects all workers that are not in the slice
|
|
// of worker ids provided but are tracked in this connected state.
|
|
func (s *connectedState) DisconnectMissingWorkers(workers []string) {
|
|
aw := make(map[string]struct{}, len(workers))
|
|
for _, i := range workers {
|
|
aw[i] = struct{}{}
|
|
}
|
|
for _, w := range s.workerIds {
|
|
if _, found := aw[w]; !found {
|
|
s.dm.disconnectWorkerId(w)
|
|
}
|
|
}
|
|
}
|
|
|
|
// DisconnectMissingUnmappedKeyIds disconnects all connections which are not in
|
|
// the slice of key ids provided but are tracked in this connected state.
|
|
func (s *connectedState) DisconnectMissingUnmappedKeyIds(keyIds []string) {
|
|
am := make(map[string]struct{}, len(keyIds))
|
|
for _, i := range keyIds {
|
|
am[i] = struct{}{}
|
|
}
|
|
for _, i := range s.unmappedKeyIds {
|
|
if _, found := am[i]; !found {
|
|
s.dm.disconnectKeys(i)
|
|
}
|
|
}
|
|
}
|
|
|
|
// DisconnectAllMissingKeyIds disconnects all workers which are not in the slice
|
|
// of key ids provided but are tracked in this connected state.
|
|
// Deprecated. Please use DisconnectMissingUnmappedKeyIds and DisconnectMissingWorkers.
|
|
// This remains for now only to support older controller versions.
|
|
func (s *connectedState) DisconnectAllMissingKeyIds(keyIds []string) {
|
|
am := make(map[string]struct{}, len(keyIds))
|
|
for _, i := range keyIds {
|
|
am[i] = struct{}{}
|
|
}
|
|
for _, i := range s.allKeyIds {
|
|
if _, found := am[i]; !found {
|
|
s.dm.disconnectKeys(i)
|
|
}
|
|
}
|
|
}
|