store

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BackupBinary is a file backup format.
	BackupBinary = iota
)
View Source
const (
	SystemEnforce = ".system"
)

Variables

View Source
var (
	NamespaceExisted = errors.New("namespace already existed")
	ModelUnsetYet    = errors.New("model unset yet")
	// NamespaceNotExist namespace not exist
	NamespaceNotExist = errors.New("namespace not exist")
	// UnmarshalFailed unmarshal failed
	UnmarshalFailed = errors.New("unmarshal failed")
	// Transaction failed
	StateTransactionFailed = errors.New("state transaction failed")
)
View Source
var (
	// ErrNotLeader is returned when a node attempts to execute a leader-only
	// operation.
	ErrNotLeader = errors.New("not leader")

	// ErrStaleRead is returned if the executing the query would violate the
	// requested freshness.
	ErrStaleRead = errors.New("stale read")

	// ErrOpenTimeout is returned when the Store does not apply its initial
	// logs within the specified time.
	ErrOpenTimeout = errors.New("timeout waiting for initial logs application")

	// ErrInvalidBackupFormat is returned when the requested backup format
	// is not valid.
	ErrInvalidBackupFormat = errors.New("invalid backup format")
)

Functions

func CreateModelFormEnforcerState

func CreateModelFormEnforcerState(state EnforcerState) (model2.Model, error)

CreateModelFormEnforcerState creates enforcer state and links rule groups

func IsNewNode

func IsNewNode(raftDir string) bool

IsNewNode returns whether a node using raftDir would be a brand new node. It also means that the window this node joining a different cluster has passed.

func SnapshotHdr

func SnapshotHdr() []byte

SnapshotHdr is used to identify the snapshot protocol version. length 8 bytes

Types

type AssertionState

type AssertionState struct {
	Key       string
	Value     string
	Tokens    []string
	Policy    [][]string
	PolicyMap map[string]int
}

type AssertionStateMap

type AssertionStateMap map[string]AssertionState

type BackupFormat

type BackupFormat int

BackupFormat represents the format of database backup.

type ClusterState

type ClusterState int

ClusterState defines the possible Raft states the current node can be in

const (
	Leader ClusterState = iota
	Follower
	Candidate
	Shutdown
	Unknown
)

Represents the Raft cluster states

type EnforcerState

type EnforcerState struct {
	Model ModelState
}

func CreateEnforcerState

func CreateEnforcerState(e *casbin.DistributedEnforcer) (EnforcerState, error)

CreateEnforcerState transfers enforce state to a persistable enforce state

type FSMEnforceResponse

type FSMEnforceResponse struct {
	// contains filtered or unexported fields
}

type FSMResponse

type FSMResponse struct {
	// contains filtered or unexported fields
}

type ListNamespacesResponse

type ListNamespacesResponse struct {
	// contains filtered or unexported fields
}

type ListPoliciesOptions

type ListPoliciesOptions struct {
	Cursor  string
	Limit   int64
	Skip    int64
	Reverse bool
}

type ListPoliciesResponse

type ListPoliciesResponse struct {
	// contains filtered or unexported fields
}

type Listener

type Listener interface {
	net.Listener
	Dial(address string, timeout time.Duration) (net.Conn, error)
}

Listener is the interface expected by the Store for Transports.

type ModelState

type ModelState map[string]AssertionStateMap

type PrintModelResponse

type PrintModelResponse struct {
	// contains filtered or unexported fields
}

type Server

type Server struct {
	ID   string `json:"id,omitempty"`
	Addr string `json:"addr,omitempty"`
}

Server represents another node in the cluster.

type Servers

type Servers []*Server

Servers is a set of Servers.

func (Servers) Len

func (s Servers) Len() int

func (Servers) Less

func (s Servers) Less(i, j int) bool

func (Servers) Swap

func (s Servers) Swap(i, j int)

type Store

type Store struct {
	ShutdownOnRemove   bool
	SnapshotThreshold  uint64
	SnapshotInterval   time.Duration
	LeaderLeaseTimeout time.Duration
	HeartbeatTimeout   time.Duration
	ElectionTimeout    time.Duration
	ApplyTimeout       time.Duration
	RaftLogLevel       string
	// contains filtered or unexported fields
}

Store is casbin memory data, where all changes are made via Raft consensus.

func New

func New(ln Listener, c *StoreConfig) *Store

New returns a new Store.

func (*Store) AddPolicies

func (s *Store) AddPolicies(ctx context.Context, ns string, sec string, pType string, rules [][]string) ([][]string, error)

AddPolicies implements the casbin.Adapter interface.

func (*Store) Addr

func (s *Store) Addr() string

Addr returns the address of the store.

func (*Store) Apply

func (s *Store) Apply(l *raft.Log) (e interface{})

func (*Store) AuthType

func (s *Store) AuthType() auth.AuthType

func (*Store) Check

func (s *Store) Check(username, password string) bool

Check validates username and password

func (*Store) ClearPolicy

func (s *Store) ClearPolicy(ctx context.Context, ns string) error

ClearPolicy implements the casbin.Adapter interface.

func (*Store) Close

func (s *Store) Close(wait bool) error

Close closes the store. If wait is true, waits for a graceful shutdown.

func (*Store) CreateNamespace

func (s *Store) CreateNamespace(ctx context.Context, ns string) error

CreateNamespace creates a new namespace.

func (*Store) Enforce

func (s *Store) Enforce(ctx context.Context, ns string, level command.EnforcePayload_Level, freshness int64, params ...interface{}) (bool, error)

Enforce executes enforcement.

func (*Store) ID

func (s *Store) ID() string

ID returns the Raft ID of the store.

func (*Store) InitAuth

func (s *Store) InitAuth(ctx context.Context, rootUsername string) error

func (*Store) InitRoot

func (s *Store) InitRoot(username, password string) error

InitRoot init a root account

func (*Store) IsLeader

func (s *Store) IsLeader() bool

IsLeader is used to determine if the current node is cluster leader

func (*Store) Join

func (s *Store) Join(id, addr string, voter bool, metadata map[string]string) error

Join joins a node, identified by id and located at addr, to this store. The node must be ready to respond to Raft communications at that address.

func (*Store) LeaderAddr

func (s *Store) LeaderAddr() string

LeaderAddr returns the address of the current leader. Returns a blank string if there is no leader.

func (*Store) LeaderID

func (s *Store) LeaderID() (string, error)

LeaderID returns the node ID of the Raft leader. Returns a blank string if there is no leader, or an error.

func (*Store) ListNamespace

func (s *Store) ListNamespace(ctx context.Context) ([]string, error)

func (*Store) ListPolicies

func (s *Store) ListPolicies(ctx context.Context, namespace, cursor string, skip, limit int64, reverse bool) ([][]string, error)

func (*Store) Metadata

func (s *Store) Metadata(id, key string) string

Metadata returns the value for a given key, for a given node ID.

func (*Store) Nodes

func (s *Store) Nodes() ([]*Server, error)

Nodes returns the slice of nodes in the cluster, sorted by ID ascending.

func (*Store) Open

func (s *Store) Open(enableBootstrap bool) error

Open opens the Store. If enableBootstrap is set, then this node becomes a standalone node. If not set, then the calling layer must know that this node has pre-existing state, or the calling layer will trigger a join operation after opening the Store.

func (*Store) Path

func (s *Store) Path() string

Path returns the path to the store's storage directory.

func (*Store) Policies

func (s *Store) Policies(ctx context.Context, ns string, options *ListPoliciesOptions) ([][]string, error)

Policies list policies

func (*Store) PrintModel

func (s *Store) PrintModel(ctx context.Context, namespace string) (string, error)

func (*Store) Remove

func (s *Store) Remove(id string) error

Remove removes a node from the store, specified by ID.

func (*Store) RemoveFilteredPolicy

func (s *Store) RemoveFilteredPolicy(ctx context.Context, ns string, sec string, pType string, fi int32, fv []string) ([][]string, error)

RemoveFilteredPolicy implements the casbin.Adapter interface.

func (*Store) RemovePolicies

func (s *Store) RemovePolicies(ctx context.Context, ns string, sec string, pType string, rules [][]string) ([][]string, error)

RemovePolicies implements the casbin.Adapter interface.

func (*Store) Restore

func (s *Store) Restore(closer io.ReadCloser) error

Restore restores form a preexisted states

func (*Store) SetMetadata

func (s *Store) SetMetadata(md map[string]string) error

SetMetadata adds the metadata md to any existing metadata for this node.

func (*Store) SetModelFromString

func (s *Store) SetModelFromString(ctx context.Context, ns string, text string) error

SetModelFromString sets casbin model from string.

func (*Store) Snapshot

func (s *Store) Snapshot() (raft.FSMSnapshot, error)

Snapshot creates a persistable state for application

func (*Store) State

func (s *Store) State() ClusterState

State returns the current node's Raft state

func (*Store) Stats

func (s *Store) Stats() (map[string]interface{}, error)

Stats returns stats for the store.

func (*Store) UpdatePolicies

func (s *Store) UpdatePolicies(ctx context.Context, ns string, sec string, pType string, nr, or [][]string) (bool, error)

UpdatePolicies implements the casbin.Adapter interface.

func (*Store) WaitForApplied

func (s *Store) WaitForApplied(timeout time.Duration) error

WaitForApplied waits for all Raft log entries to to be applied to the underlying database.

func (*Store) WaitForAppliedIndex

func (s *Store) WaitForAppliedIndex(idx uint64, timeout time.Duration) error

WaitForAppliedIndex blocks until a given log index has been applied, or the timeout expires.

func (*Store) WaitForLeader

func (s *Store) WaitForLeader(timeout time.Duration) (string, error)

WaitForLeader blocks until a leader is detected, or the timeout expires.

type StoreConfig

type StoreConfig struct {
	Dir      string      // The working directory for raft.
	Tn       Transport   // The underlying Transport for raft.
	ID       string      // Node ID.
	Logger   *log.Logger // The logger to use to log stuff.
	AuthType auth.AuthType
	*auth.CredentialsStore
	AdvAddr string
}

StoreConfig represents the configuration of the underlying Store.

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is the network service provided to Raft, and wraps a Listener.

func NewTransport

func NewTransport(ln Listener) *Transport

NewTransport returns an initialized Transport.

func (*Transport) Accept

func (t *Transport) Accept() (net.Conn, error)

Accept waits for the next connection.

func (*Transport) Addr

func (t *Transport) Addr() net.Addr

Addr returns the binding address of the transport.

func (*Transport) Close

func (t *Transport) Close() error

Close closes the transport

func (*Transport) Dial

func (t *Transport) Dial(addr raft.ServerAddress, timeout time.Duration) (net.Conn, error)

Dial creates a new network connection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL