Commit 00ddf067 authored by Hongchao Deng's avatar Hongchao Deng

etcd (v3) store: implements KV methods of storage.Interface

This implements Get(), Create(), Delete(), GetToList(), List(), GuaranteedUpdate().
parent e2ef27ee
...@@ -169,3 +169,13 @@ func (m MultiObjectTyper) IsUnversioned(obj Object) (bool, bool) { ...@@ -169,3 +169,13 @@ func (m MultiObjectTyper) IsUnversioned(obj Object) (bool, bool) {
} }
return false, false return false, false
} }
// SetZeroValue would set the object of objPtr to zero value of its type.
func SetZeroValue(objPtr Object) error {
v, err := conversion.EnforcePtr(objPtr)
if err != nil {
return err
}
v.Set(reflect.Zero(v.Type()))
return nil
}
...@@ -26,6 +26,7 @@ const ( ...@@ -26,6 +26,7 @@ const (
ErrCodeKeyNotFound int = iota + 1 ErrCodeKeyNotFound int = iota + 1
ErrCodeKeyExists ErrCodeKeyExists
ErrCodeResourceVersionConflicts ErrCodeResourceVersionConflicts
ErrCodeInvalidObj
ErrCodeUnreachable ErrCodeUnreachable
) )
...@@ -33,6 +34,7 @@ var errCodeToMessage = map[int]string{ ...@@ -33,6 +34,7 @@ var errCodeToMessage = map[int]string{
ErrCodeKeyNotFound: "key not found", ErrCodeKeyNotFound: "key not found",
ErrCodeKeyExists: "key exists", ErrCodeKeyExists: "key exists",
ErrCodeResourceVersionConflicts: "resource version conflicts", ErrCodeResourceVersionConflicts: "resource version conflicts",
ErrCodeInvalidObj: "invalid object",
ErrCodeUnreachable: "server unreachable", ErrCodeUnreachable: "server unreachable",
} }
...@@ -68,15 +70,24 @@ func NewUnreachableError(key string, rv int64) *StorageError { ...@@ -68,15 +70,24 @@ func NewUnreachableError(key string, rv int64) *StorageError {
} }
} }
func NewInvalidObjError(key, msg string) *StorageError {
return &StorageError{
Code: ErrCodeInvalidObj,
Key: key,
AdditionalErrorMsg: msg,
}
}
type StorageError struct { type StorageError struct {
Code int Code int
Key string Key string
ResourceVersion int64 ResourceVersion int64
AdditionalErrorMsg string
} }
func (e *StorageError) Error() string { func (e *StorageError) Error() string {
return fmt.Sprintf("StorageError: %s, Code: %d, Key: %s, ResourceVersion: %d", return fmt.Sprintf("StorageError: %s, Code: %d, Key: %s, ResourceVersion: %d, AdditionalErrorMsg: %s",
errCodeToMessage[e.Code], e.Code, e.Key, e.ResourceVersion) errCodeToMessage[e.Code], e.Code, e.Key, e.ResourceVersion, e.AdditionalErrorMsg)
} }
// IsNotFound returns true if and only if err is "key" not found error. // IsNotFound returns true if and only if err is "key" not found error.
...@@ -96,15 +107,24 @@ func IsUnreachable(err error) bool { ...@@ -96,15 +107,24 @@ func IsUnreachable(err error) bool {
// IsTestFailed returns true if and only if err is a write conflict. // IsTestFailed returns true if and only if err is a write conflict.
func IsTestFailed(err error) bool { func IsTestFailed(err error) bool {
return isErrCode(err, ErrCodeResourceVersionConflicts) return isErrCode(err, ErrCodeResourceVersionConflicts, ErrCodeInvalidObj)
}
// IsInvalidUID returns true if and only if err is invalid UID error
func IsInvalidObj(err error) bool {
return isErrCode(err, ErrCodeInvalidObj)
} }
func isErrCode(err error, code int) bool { func isErrCode(err error, codes ...int) bool {
if err == nil { if err == nil {
return false return false
} }
if e, ok := err.(*StorageError); ok { if e, ok := err.(*StorageError); ok {
return e.Code == code for _, code := range codes {
if e.Code == code {
return true
}
}
} }
return false return false
} }
......
...@@ -104,6 +104,7 @@ type Interface interface { ...@@ -104,6 +104,7 @@ type Interface interface {
Set(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error Set(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error
// Delete removes the specified key and returns the value that existed at that spot. // Delete removes the specified key and returns the value that existed at that spot.
// If key didn't exist, it will return NotFound storage error.
Delete(ctx context.Context, key string, out runtime.Object, preconditions *Preconditions) error Delete(ctx context.Context, key string, out runtime.Object, preconditions *Preconditions) error
// Watch begins watching the specified key. Events are decoded into API objects, // Watch begins watching the specified key. Events are decoded into API objects,
...@@ -137,9 +138,13 @@ type Interface interface { ...@@ -137,9 +138,13 @@ type Interface interface {
// GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'ptrToType') // GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'ptrToType')
// retrying the update until success if there is index conflict. // retrying the update until success if there is index conflict.
// Note that object passed to tryUpdate may change acress incovations of tryUpdate() if // Note that object passed to tryUpdate may change across invocations of tryUpdate() if
// other writers are simultaneously updateing it, to tryUpdate() needs to take into account // other writers are simultaneously updating it, to tryUpdate() needs to take into account
// the current contents of the object when deciding how the update object should look. // the current contents of the object when deciding how the update object should look.
// If the key doesn't exist, it will return NotFound storage error if ignoreNotFound=false
// or zero value in 'ptrToType' parameter otherwise.
// If the object to update has the same value as previous, it won't do any update
// but will return the object in 'ptrToType' parameter.
// //
// Example: // Example:
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment