Skip to content
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

fix: remove closeChannelWhenEmpty busy loop #2594

Merged
merged 13 commits into from
Nov 10, 2024
6 changes: 2 additions & 4 deletions cli/cli/commands/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/multi_os_command_executor"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/user_support_constants"
"github.com/kurtosis-tech/kurtosis/contexts-config-store/store"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
)

var WebCmd = &lowlevel.LowlevelKurtosisCommand{
Expand All @@ -35,9 +35,7 @@ func run(_ context.Context, _ *flags.ParsedFlags, _ *args.ParsedArgs) error {
return stacktrace.Propagate(err, "tried fetching the current Kurtosis context but failed, we can't switch clusters without this information. This is a bug in Kurtosis")
}
if store.IsRemote(currentKurtosisContext) {
if err := multi_os_command_executor.OpenFile(user_support_constants.KurtosisCloudLink); err != nil {
return stacktrace.Propagate(err, "An error occurred while opening the Kurtosis Cloud Web UI")
}
logrus.Warn("Kurtosis Cloud has been deprecated. Switch to a local kurtosis context to use the local Kurtosis web UI instead.")
}

if err := multi_os_command_executor.OpenFile(webUILink); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
enclaveManagerUIPort = 9711
enclaveManagerAPIPort = 8081
engineDebugServerPort = 50102 // in ClI this is 50101 and 50103 for the APIC
pprofServerPort = 6060
maxWaitForEngineAvailabilityRetries = 40
timeBetweenWaitForEngineAvailabilityRetries = 2 * time.Second
logsStorageDirPath = "/var/log/kurtosis/"
Expand Down Expand Up @@ -169,6 +170,16 @@ func CreateEngine(
)
}

pprofPortSpec, err := port_spec.NewPortSpec(pprofServerPort, consts.EngineTransportProtocol, consts.HttpApplicationProtocol, defaultWait, consts.EmptyApplicationURL)
if err != nil {
return nil, stacktrace.Propagate(
err,
"An error occurred creating the pprof http port spec object using number '%v' and protocol '%v'",
pprofServerPort,
consts.EngineTransportProtocol.String(),
)
}

engineAttrs, err := objAttrsProvider.ForEngineServer(
engineGuid,
consts.KurtosisInternalContainerGrpcPortId,
Expand Down Expand Up @@ -205,11 +216,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(pprofServerPort),
}

// Configure the debug port only if it's required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const (
FeedbackEmail = "feedback@" + OldDomain
FeedbackEmailLink = "mailto:" + FeedbackEmail
KurtosisTechTwitterProfileLink = "https://twitter.com/KurtosisTech"
KurtosisCloudLink = "https://cloud." + Domain

// If you add new URLs above, make sure to add them to the urlsToValidateInTest below!!!
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
Expand All @@ -56,6 +55,5 @@ var urlsToValidateInTest = []string{
KurtosisDiscordUrl,
KurtosisOnBoardCalendlyUrl,
HowImportWorksLink,
KurtosisCloudLink,
KurtosisTechTwitterProfileLink,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 buffer to the channel to be read
logLineSender.Flush()

// wait until the channel has been fully read/empty before closing it
closeChannelWhenEmpty(logsByKurtosisUserServiceUuidChan)
close(logsByKurtosisUserServiceUuidChan)
close(streamErrChan)

//then cancel the context
Expand Down Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,13 @@ func executeStreamCallAndGetReceivedServiceLogLines(
case <-time.Tick(testTimeOut):
return nil, stacktrace.NewError("Receiving stream logs in the test has reached the '%v' time out", testTimeOut)
case streamErr, isChanOpen := <-errChan:
if !isChanOpen {
if !isChanOpen && len(userServiceLogsByUuidChan) == 0 {
shouldReceiveStream = false
break
}
return nil, stacktrace.Propagate(streamErr, "Receiving streaming error.")
if isChanOpen && streamErr != nil {
return nil, stacktrace.Propagate(streamErr, "Receiving streaming error.")
}
case userServiceLogsByUuid, isChanOpen := <-userServiceLogsByUuidChan:
if !isChanOpen {
shouldReceiveStream = false
Expand Down
32 changes: 20 additions & 12 deletions engine/server/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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 (
Expand All @@ -73,6 +73,7 @@ const (
functionPathSeparator = "."
emptyFunctionName = ""
webappPortAddr = ":9711"
pprofPortAddr = ":6060"

remoteBackendConfigFilename = "remote_backend_config.json"
pathToStaticFolder = "/run/webapp"
Expand Down Expand Up @@ -200,6 +201,13 @@ func runMain() error {
return stacktrace.Propagate(err, "Failed to create an enclave manager for backend type '%v' and config '%+v'", serverArgs.KurtosisBackendType, backendConfig)
}

go func() {
logrus.Infof("Starting pprof server on: %s", pprofPortAddr)
if err := http.ListenAndServe(pprofPortAddr, nil); err != nil {
logrus.Fatalf("pprof server failed: %v", err)
}
}()

go func() {
envJsFilePath := filepath.Join(pathToStaticFolder, envJsFilename)
envJsFilePathPerm := envJsFilePerm
Expand Down
6 changes: 4 additions & 2 deletions engine/server/engine/server/engine_connect_server_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,10 @@ func (service *EngineConnectServerService) GetServiceLogs(ctx context.Context, c
logrus.Debug("Exiting the stream because an error from the logs database client was received through the error chan.")
return stacktrace.Propagate(err, "An error occurred streaming user service logs.")
}
logrus.Debug("Exiting the stream loop after receiving a close signal from the error chan")
return nil
if len(serviceLogsByServiceUuidChan) == 0 {
logrus.Debug("Exiting the stream loop after receiving a close signal from the error chan")
return nil
}
}
}
}
Expand Down
Loading