From 94952cd1c7c7a96aeb41a728f360fe0c162f4893 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Thu, 7 Nov 2024 10:47:38 -0800 Subject: [PATCH] remove closeChannelWhenEmpty busy loop --- .../engine_functions/create_engine.go | 16 ++++++++++ .../persistent_volume_logs_database_client.go | 16 ++-------- ...istent_volume_logs_database_client_test.go | 4 ++- engine/server/engine/main.go | 32 ++++++++++++------- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/engine_functions/create_engine.go b/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/engine_functions/create_engine.go index 3096451b91..0cc596c907 100644 --- a/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/engine_functions/create_engine.go +++ b/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/engine_functions/create_engine.go @@ -169,6 +169,16 @@ func CreateEngine( ) } + pprofPortSpec, err := port_spec.NewPortSpec(6060, consts.EngineTransportProtocol, consts.HttpApplicationProtocol, defaultWait, consts.EmptyApplicationURL) + if err != nil { + return nil, stacktrace.Propagate( + err, + "An error occurred creating the pprofs http port spec object using number '%v' and protocol '%v'", + 6060, + consts.EngineTransportProtocol.String(), + ) + } + engineAttrs, err := objAttrsProvider.ForEngineServer( engineGuid, consts.KurtosisInternalContainerGrpcPortId, @@ -205,11 +215,17 @@ func CreateEngine( return nil, stacktrace.Propagate(err, "An error occurred transforming the Enclave Manager API port spec to a Docker port") } + pprofDockerPort, err := shared_helpers.TransformPortSpecToDockerPort(pprofPortSpec) + if err != nil { + return nil, stacktrace.Propagate(err, "An error occurred transforming the pprof port spec to a Docker port") + } + usedPorts := map[nat.Port]docker_manager.PortPublishSpec{ privateGrpcDockerPort: docker_manager.NewManualPublishingSpec(grpcPortNum), enclaveManagerUIDockerPort: docker_manager.NewManualPublishingSpec(uint16(enclaveManagerUIPort)), enclaveManagerAPIDockerPort: docker_manager.NewManualPublishingSpec(uint16(enclaveManagerAPIPort)), restAPIDockerPort: docker_manager.NewManualPublishingSpec(engine.RESTAPIPortAddr), + pprofDockerPort: docker_manager.NewManualPublishingSpec(6060), } // Configure the debug port only if it's required diff --git a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client.go b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client.go index 67bd58b6bb..16497b53c5 100644 --- a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client.go +++ b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client.go @@ -90,14 +90,13 @@ func (client *persistentVolumeLogsDatabaseClient) StreamUserServiceLogs( // this go routine handles the stream cancellation go func() { - //wait for stream go routine to end + // wait for stream go routine to end wgSenders.Wait() - // send all buffered log lines + // flush should send remainder of logs in the channel logLineSender.Flush() - // wait until the channel has been fully read/empty before closing it - closeChannelWhenEmpty(logsByKurtosisUserServiceUuidChan) + close(logsByKurtosisUserServiceUuidChan) close(streamErrChan) //then cancel the context @@ -174,12 +173,3 @@ func (client *persistentVolumeLogsDatabaseClient) streamServiceLogLines( shouldReturnAllLogs, numLogLines) } - -func closeChannelWhenEmpty(logsChan chan map[service.ServiceUUID][]logline.LogLine) { - for { - if len(logsChan) == 0 { - close(logsChan) - return - } - } -} diff --git a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client_test.go b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client_test.go index d245581bde..21e898e30c 100644 --- a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client_test.go +++ b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/persistent_volume_logs_database_client_test.go @@ -722,7 +722,9 @@ func executeStreamCallAndGetReceivedServiceLogLines( return nil, stacktrace.NewError("Receiving stream logs in the test has reached the '%v' time out", testTimeOut) case streamErr, isChanOpen := <-errChan: if !isChanOpen { - shouldReceiveStream = false + if len(userServiceLogsByUuidChan) == 0 { + shouldReceiveStream = false + } break } return nil, stacktrace.Propagate(streamErr, "Receiving streaming error.") diff --git a/engine/server/engine/main.go b/engine/server/engine/main.go index 4670968c54..69b21c42f5 100644 --- a/engine/server/engine/main.go +++ b/engine/server/engine/main.go @@ -8,18 +8,6 @@ package main import ( "context" "fmt" - "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout" - "io/fs" - "math" - "net" - "net/http" - "os" - "path" - "path/filepath" - "runtime" - "strings" - "time" - "github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings" "github.com/kurtosis-tech/kurtosis/api/golang/engine/kurtosis_engine_rpc_api_bindings/kurtosis_engine_rpc_api_bindingsconnect" enclaveApi "github.com/kurtosis-tech/kurtosis/api/golang/http_rest/server/core_rest_api" @@ -39,6 +27,7 @@ import ( "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs" "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/kurtosis_backend" "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume" + "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout" "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/log_file_manager" "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/logs_clock" "github.com/kurtosis-tech/kurtosis/engine/server/engine/centralized_logs/client_implementations/persistent_volume/stream_logs_strategy" @@ -57,6 +46,17 @@ import ( echomiddleware "github.com/labstack/echo/v4/middleware" "github.com/rs/cors" "github.com/sirupsen/logrus" + "io/fs" + "math" + "net" + "net/http" + _ "net/http/pprof" + "os" + "path" + "path/filepath" + "runtime" + "strings" + "time" ) const ( @@ -200,6 +200,14 @@ func runMain() error { return stacktrace.Propagate(err, "Failed to create an enclave manager for backend type '%v' and config '%+v'", serverArgs.KurtosisBackendType, backendConfig) } + // start server + go func() { + logrus.Info("Starting pprof server on :6060") + if err := http.ListenAndServe("0.0.0.0:6060", nil); err != nil { + logrus.Fatalf("pprof server failed: %v", err) + } + }() + go func() { envJsFilePath := filepath.Join(pathToStaticFolder, envJsFilename) envJsFilePathPerm := envJsFilePerm