-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialize chain elements #583
Merged
denis-tingaikin
merged 12 commits into
networkservicemesh:master
from
Bolodya1997:serialize
Dec 1, 2020
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d0c26f5
Add serialize
8066a6b
Rework timeout with serialize
7e9494b
Rework serialize with RequestExecutor, CloseExecutor
c78efe9
Fix race condition issue in serialize
e2dcb08
Update github.com/edwarnicke/serialize version
16b3135
Rework serialize
9e79c2e
Fix URL in README
8238a71
Use atomic.AddUint32() instead of uuid.New().Id()
8cb3305
Add multiexecutor
406d0c0
Rework serialize with multi executor
f8a5f54
Move multi executor to serialize
3978cab
Rename `IExecutor` to `Executor`
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Functional requirements | ||
|
||
`Request`, `Close` events for the same `Connection.ID` should be executed in network service chain serially. | ||
|
||
# Implementation | ||
|
||
## serializeServer, serializeClient | ||
|
||
Serialize chain elements use [multi executor](https://github.com/networkservicemesh/sdk/blob/master/pkg/tools/multiexecutor/multi_executor.go) | ||
and stores per-id executor in the context. | ||
|
||
Correct event firing chain element example: | ||
```go | ||
func (s *eventServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { | ||
executor := serialize.Executor(ctx) | ||
go func() { | ||
executor.AsyncExec(func() { | ||
_, _ = next.Server(ctx).Request(serialize.WithExecutor(context.TODO(), executor), request) | ||
}) | ||
}() | ||
|
||
conn, err := next.Server(ctx).Request(ctx, request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
go func() { | ||
executor.AsyncExec(func() { | ||
_, _ = next.Server(ctx).Close(serialize.WithExecutor(context.TODO(), executor), conn) | ||
}) | ||
}() | ||
|
||
return conn, nil | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 serialize | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
) | ||
|
||
type serializeClient struct { | ||
executor multiExecutor | ||
} | ||
|
||
// NewClient returns a new serialize client chain element | ||
func NewClient() networkservice.NetworkServiceClient { | ||
return new(serializeClient) | ||
} | ||
|
||
func (c *serializeClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) { | ||
connID := request.GetConnection().GetId() | ||
<-c.executor.AsyncExec(connID, func() { | ||
conn, err = next.Client(ctx).Request(WithExecutor(ctx, c.executor.Executor(connID)), request, opts...) | ||
}) | ||
return conn, err | ||
} | ||
|
||
func (c *serializeClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (_ *empty.Empty, err error) { | ||
<-c.executor.AsyncExec(conn.GetId(), func() { | ||
_, err = next.Client(ctx).Close(WithExecutor(ctx, c.executor.Executor(conn.GetId())), conn, opts...) | ||
}) | ||
return new(empty.Empty), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 serialize | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
const ( | ||
executorKey contextKeyType = "executor" | ||
) | ||
|
||
type contextKeyType string | ||
|
||
// IExecutor is a serialize.Executor interface type | ||
type IExecutor interface { | ||
AsyncExec(f func()) <-chan struct{} | ||
} | ||
|
||
// WithExecutor wraps `parent` in a new context with IExecutor | ||
func WithExecutor(parent context.Context, executor IExecutor) context.Context { | ||
if parent == nil { | ||
panic("cannot create context from nil parent") | ||
} | ||
return context.WithValue(parent, executorKey, executor) | ||
} | ||
|
||
// Executor returns IExecutor | ||
func Executor(ctx context.Context) IExecutor { | ||
if executor, ok := ctx.Value(executorKey).(IExecutor); ok { | ||
return executor | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 serialize | ||
|
||
type executorFunc func(f func()) <-chan struct{} | ||
|
||
func (e executorFunc) AsyncExec(f func()) <-chan struct{} { | ||
return e(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 serialize | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/edwarnicke/serialize" | ||
) | ||
|
||
type multiExecutor struct { | ||
executors map[string]*refCountExecutor | ||
executor serialize.Executor | ||
once sync.Once | ||
} | ||
|
||
type refCountExecutor struct { | ||
count int | ||
executor serialize.Executor | ||
} | ||
|
||
func (e *multiExecutor) AsyncExec(id string, f func()) (ch <-chan struct{}) { | ||
e.once.Do(func() { | ||
e.executors = make(map[string]*refCountExecutor) | ||
}) | ||
|
||
<-e.executor.AsyncExec(func() { | ||
exec, ok := e.executors[id] | ||
if !ok { | ||
exec = new(refCountExecutor) | ||
e.executors[id] = exec | ||
} | ||
exec.count++ | ||
|
||
ch = exec.executor.AsyncExec(func() { | ||
f() | ||
e.executor.AsyncExec(func() { | ||
exec.count-- | ||
if exec.count == 0 { | ||
delete(e.executors, id) | ||
} | ||
}) | ||
}) | ||
}) | ||
return ch | ||
} | ||
|
||
func (e *multiExecutor) Executor(id string) IExecutor { | ||
return executorFunc(func(f func()) <-chan struct{} { | ||
return e.AsyncExec(id, f) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 serialize provides chain elements for serial Request, Close event processing | ||
package serialize | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
) | ||
|
||
type serializeServer struct { | ||
executor multiExecutor | ||
} | ||
|
||
// NewServer returns a new serialize server chain element | ||
func NewServer() networkservice.NetworkServiceServer { | ||
return new(serializeServer) | ||
} | ||
|
||
func (s *serializeServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (conn *networkservice.Connection, err error) { | ||
connID := request.GetConnection().GetId() | ||
<-s.executor.AsyncExec(connID, func() { | ||
conn, err = next.Server(ctx).Request(WithExecutor(ctx, s.executor.Executor(connID)), request) | ||
}) | ||
return conn, err | ||
} | ||
|
||
func (s *serializeServer) Close(ctx context.Context, conn *networkservice.Connection) (_ *empty.Empty, err error) { | ||
<-s.executor.AsyncExec(conn.GetId(), func() { | ||
_, err = next.Server(ctx).Close(WithExecutor(ctx, s.executor.Executor(conn.GetId())), conn) | ||
}) | ||
return new(empty.Empty), err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please lets follow go interface naming convention https://golang.org/doc/effective_go.html#interface-names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed: