This is an automated email from the ASF dual-hosted git repository.

ka94 pushed a commit to branch issues/6401
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit df67e9f40b5eadb3659efa36bfa9f575c7afbf4d
Author: Keon Amini <[email protected]>
AuthorDate: Sat Nov 11 19:48:39 2023 -0600

    fix: use streaming for Python->Go RemoteScopes API to prevent oversized 
payloads
---
 backend/go.mod                                     |  4 ++++
 backend/go.sum                                     |  9 ++++++++
 backend/python/pydevlake/pydevlake/ipc.py          |  2 +-
 backend/python/pydevlake/pydevlake/plugin.py       |  8 ++-----
 .../services/remote/plugin/remote_scope_api.go     | 26 ++++++++++++++++------
 5 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/backend/go.mod b/backend/go.mod
index 4894eba52..adff64ec2 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -93,7 +93,9 @@ require (
        github.com/kr/text v0.2.0 // indirect
        github.com/leodido/go-urn v1.2.4 // indirect
        github.com/mailru/easyjson v0.7.7 // indirect
+       github.com/mattn/go-colorable v0.1.11 // indirect
        github.com/mattn/go-isatty v0.0.19 // indirect
+       github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
        github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // 
indirect
        github.com/modern-go/reflect2 v1.0.2 // indirect
        github.com/pborman/uuid v1.2.1 // indirect
@@ -119,6 +121,7 @@ require (
        golang.org/x/arch v0.3.0 // indirect
        golang.org/x/net v0.16.0 // indirect
        golang.org/x/sys v0.13.0 // indirect
+       golang.org/x/term v0.13.0 // indirect
        golang.org/x/text v0.13.0 // indirect
        golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
        golang.org/x/tools v0.14.0 // indirect
@@ -134,6 +137,7 @@ require (
 
 require (
        github.com/golang-jwt/jwt/v5 v5.0.0-rc.1
+       github.com/x-cray/logrus-prefixed-formatter v0.5.2
        golang.org/x/mod v0.13.0
 )
 
diff --git a/backend/go.sum b/backend/go.sum
index 1f8c8abd5..ac1d063a7 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -486,6 +486,7 @@ github.com/mattn/go-colorable v0.1.1/go.mod 
h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
 github.com/mattn/go-colorable v0.1.2/go.mod 
h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
 github.com/mattn/go-colorable v0.1.6/go.mod 
h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
 github.com/mattn/go-colorable v0.1.8/go.mod 
h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
+github.com/mattn/go-colorable v0.1.11 
h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
 github.com/mattn/go-colorable v0.1.11/go.mod 
h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
 github.com/mattn/go-isatty v0.0.3/go.mod 
h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
 github.com/mattn/go-isatty v0.0.5/go.mod 
h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
@@ -504,6 +505,8 @@ github.com/mediocregopher/radix/v3 v3.3.0/go.mod 
h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQ
 github.com/mediocregopher/radix/v3 v3.4.2/go.mod 
h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8=
 github.com/merico-dev/graphql v0.0.0-20221027131946-77460a1fd4cd 
h1:hGQXd4a72JSFIZE+ZVkH5ivE925PGogjob6stgc2too=
 github.com/merico-dev/graphql v0.0.0-20221027131946-77460a1fd4cd/go.mod 
h1:dcDqG8HXVtfEhTCipFMa0Q+RTKTtDKIO2vJt+JVzHEQ=
+github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d 
h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
+github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod 
h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
 github.com/microcosm-cc/bluemonday v1.0.2/go.mod 
h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
 github.com/miekg/dns v1.0.14/go.mod 
h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
 github.com/mitchellh/cli v1.0.0/go.mod 
h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -532,10 +535,12 @@ github.com/nats-io/nkeys v0.0.2/go.mod 
h1:dab7URMsZm6Z/jp9Z5UGa87Uutgc2mVpXLC4B7
 github.com/nats-io/nkeys v0.1.0/go.mod 
h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
 github.com/nats-io/nuid v1.0.1/go.mod 
h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod 
h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
+github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
 github.com/nxadm/tail v1.4.4/go.mod 
h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
 github.com/onsi/ginkgo v1.6.0/go.mod 
h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.10.3/go.mod 
h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.12.1/go.mod 
h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
+github.com/onsi/ginkgo v1.13.0 h1:M76yO2HkZASFjXL0HSoZJ1AYEmQxNJmY41Jx1zNUq1Y=
 github.com/onsi/ginkgo v1.13.0/go.mod 
h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0=
 github.com/onsi/gomega v1.7.1/go.mod 
h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
 github.com/onsi/gomega v1.10.1/go.mod 
h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
@@ -673,6 +678,8 @@ github.com/valyala/fasttemplate v1.2.1/go.mod 
h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+
 github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod 
h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
 github.com/viant/afs v1.16.0 h1:yb9TQ1gjVVLji9lcXLWaarklqmGWeXTZOwc2fwJevCI=
 github.com/viant/afs v1.16.0/go.mod 
h1:wdiEDffZKJwj1ZSFasy7hHoxLQdSpFZkd3XOWNt1aN0=
+github.com/x-cray/logrus-prefixed-formatter v0.5.2 
h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
+github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod 
h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
 github.com/xanzy/ssh-agent v0.3.3 
h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
 github.com/xanzy/ssh-agent v0.3.3/go.mod 
h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
 github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod 
h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
@@ -945,6 +952,7 @@ golang.org/x/term v0.2.0/go.mod 
h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
 golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
 golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
 golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
+golang.org/x/term v0.13.0/go.mod 
h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
 golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod 
h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod 
h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -1165,6 +1173,7 @@ gopkg.in/ini.v1 v1.51.1/go.mod 
h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
 gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod 
h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
+gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 
h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod 
h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
 gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
 gopkg.in/warnings.v0 v0.1.2/go.mod 
h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
diff --git a/backend/python/pydevlake/pydevlake/ipc.py 
b/backend/python/pydevlake/pydevlake/ipc.py
index ee308189d..cfa796e50 100644
--- a/backend/python/pydevlake/pydevlake/ipc.py
+++ b/backend/python/pydevlake/pydevlake/ipc.py
@@ -106,7 +106,7 @@ class PluginCommands:
     @plugin_method
     def remote_scopes(self, connection: dict, group_id: Optional[str] = None):
         c = self._plugin.connection_type(**connection)
-        return self._plugin.make_remote_scopes(c, group_id)
+        yield from self._plugin.make_remote_scopes(c, group_id)
 
     def _mk_context(self, data: dict):
         db_url = data['db_url']
diff --git a/backend/python/pydevlake/pydevlake/plugin.py 
b/backend/python/pydevlake/pydevlake/plugin.py
index 4d2f4993f..fc368261a 100644
--- a/backend/python/pydevlake/pydevlake/plugin.py
+++ b/backend/python/pydevlake/pydevlake/plugin.py
@@ -114,22 +114,18 @@ class Plugin(ABC):
 
     def make_remote_scopes(self, connection: Connection, group_id: 
Optional[str] = None) -> msg.RemoteScopes:
         if group_id:
-            remote_scopes = []
             for tool_scope in self.remote_scopes(connection, group_id):
                 tool_scope.connection_id = connection.id
                 tool_scope.raw_data_params = raw_data_params(connection.id, 
tool_scope.id)
                 tool_scope.raw_data_table = self._raw_scope_table_name()
-                remote_scopes.append(
-                    msg.RemoteScope(
+                yield msg.RemoteScope(
                         id=tool_scope.id,
                         parent_id=group_id,
                         name=tool_scope.name,
                         data=tool_scope
                     )
-                )
         else:
-            remote_scopes = self.remote_scope_groups(connection)
-        return msg.RemoteScopes(__root__=remote_scopes)
+            yield from self.remote_scope_groups(connection)
 
     def make_pipeline(self, scope_config_pairs: list[ScopeConfigPair],
                       connection: Connection) -> msg.PipelineData:
diff --git a/backend/server/services/remote/plugin/remote_scope_api.go 
b/backend/server/services/remote/plugin/remote_scope_api.go
index e766a2c8b..200449b38 100644
--- a/backend/server/services/remote/plugin/remote_scope_api.go
+++ b/backend/server/services/remote/plugin/remote_scope_api.go
@@ -18,6 +18,7 @@ limitations under the License.
 package plugin
 
 import (
+       "github.com/apache/incubator-devlake/core/models"
        "net/http"
        "strconv"
 
@@ -43,28 +44,39 @@ func (pa *pluginAPI) GetRemoteScopes(input 
*plugin.ApiResourceInput) (*plugin.Ap
        if connectionId == 0 {
                return nil, errors.BadInput.New("invalid connectionId")
        }
-
        connection := pa.connType.New()
        err := pa.connhelper.First(connection, input.Params)
        if err != nil {
                return nil, err
        }
-
        groupId := input.Query.Get("groupId")
-
-       remoteScopes := make([]RemoteScopesTreeNode, 0)
-       err = pa.invoker.Call("remote-scopes", bridge.DefaultContext, 
connection.Unwrap(), groupId).Get(&remoteScopes)
+       remoteScopes, err := pa.getRemoteScopesIncrementally(connection, 
groupId)
        if err != nil {
                return nil, err
        }
-
        output := RemoteScopesOutput{
                Children: remoteScopes,
        }
-
        return &plugin.ApiResourceOutput{Body: output, Status: http.StatusOK}, 
nil
 }
 
+func (pa *pluginAPI) getRemoteScopesIncrementally(connection 
models.DynamicTabler, groupId string) ([]RemoteScopesTreeNode, errors.Error) {
+       remoteScopes := make([]RemoteScopesTreeNode, 0)
+       stream := pa.invoker.Stream("remote-scopes", bridge.DefaultContext, 
connection.Unwrap(), groupId)
+       for recv := range stream.Receive() {
+               if recv.Err != nil {
+                       return nil, recv.Err
+               }
+               scope := RemoteScopesTreeNode{}
+               err := recv.Get(&scope)
+               if err != nil {
+                       return nil, err
+               }
+               remoteScopes = append(remoteScopes, scope)
+       }
+       return remoteScopes, nil
+}
+
 func (pa *pluginAPI) SearchRemoteScopes(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput, errors.Error) {
        return &plugin.ApiResourceOutput{Status: http.StatusNotImplemented}, nil
 }

Reply via email to