-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathserver.go
More file actions
415 lines (377 loc) · 11.7 KB
/
Copy pathserver.go
File metadata and controls
415 lines (377 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Package server runs the model-runner HTTP daemon.
package server
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/docker/model-runner/pkg/envconfig"
"github.com/docker/model-runner/pkg/inference"
"github.com/docker/model-runner/pkg/inference/backends/llamacpp"
"github.com/docker/model-runner/pkg/inference/backends/sglang"
"github.com/docker/model-runner/pkg/inference/config"
"github.com/docker/model-runner/pkg/inference/models"
"github.com/docker/model-runner/pkg/logging"
dmrlogs "github.com/docker/model-runner/pkg/logs"
"github.com/docker/model-runner/pkg/metrics"
"github.com/docker/model-runner/pkg/routing"
modeltls "github.com/docker/model-runner/pkg/tls"
)
// Config holds server startup options.
type Config struct {
Version string
// ExitFunc is called on fatal errors that cannot be propagated via the
// callback signature (e.g. OnBackendError). Defaults to os.Exit.
ExitFunc func(int)
}
// Run starts the HTTP server and blocks until ctx is cancelled.
func Run(ctx context.Context, cfg Config) error {
exitFunc := cfg.ExitFunc
if exitFunc == nil {
exitFunc = os.Exit
}
log := logging.NewLogger(envconfig.LogLevel())
sockName := envconfig.SocketPath()
modelPath, err := envconfig.ModelsPath()
if err != nil {
return fmt.Errorf("failed to get models path: %w", err)
}
if envconfig.DisableServerUpdate() {
llamacpp.ShouldUpdateServerLock.Lock()
llamacpp.ShouldUpdateServer = false
llamacpp.ShouldUpdateServerLock.Unlock()
}
if v := envconfig.LlamaServerVersion(); v != "" {
llamacpp.SetDesiredServerVersion(v)
}
llamaServerPath := envconfig.LlamaServerPath()
vllmServerPath := envconfig.VLLMServerPath()
sglangServerPath := envconfig.SGLangServerPath()
mlxServerPath := envconfig.MLXServerPath()
diffusersServerPath := envconfig.DiffusersServerPath()
vllmMetalServerPath := envconfig.VLLMMetalServerPath()
// Create a proxy-aware HTTP transport.
var baseTransport *http.Transport
if t, ok := http.DefaultTransport.(*http.Transport); ok {
baseTransport = t.Clone()
} else {
baseTransport = &http.Transport{}
}
baseTransport.Proxy = http.ProxyFromEnvironment
log.Info("LLAMA_SERVER_PATH", "path", llamaServerPath)
if vllmServerPath != "" {
log.Info("VLLM_SERVER_PATH", "path", vllmServerPath)
}
if sglangServerPath != "" {
log.Info("SGLANG_SERVER_PATH", "path", sglangServerPath)
}
if mlxServerPath != "" {
log.Info("MLX_SERVER_PATH", "path", mlxServerPath)
}
if diffusersServerPath != "" {
log.Info("DIFFUSERS_SERVER_PATH", "path", diffusersServerPath)
}
if vllmMetalServerPath != "" {
log.Info("VLLM_METAL_SERVER_PATH", "path", vllmMetalServerPath)
}
// Determine log directory. When MODEL_RUNNER_LOG_DIR is set use it;
// otherwise auto-create a default directory so that the /logs endpoint is
// available in all deployment modes.
logDir := envconfig.LogDir()
if logDir == "" {
logDir = filepath.Join(os.TempDir(), "model-runner-logs")
if mkdirErr := os.MkdirAll(logDir, 0o755); mkdirErr != nil {
log.Warn("failed to create default log directory, /logs endpoint will be disabled",
"dir", logDir, "error", mkdirErr)
logDir = ""
}
}
// When a log directory is available, set up file-backed logging using tee
// writers (stderr + bracket-timestamped files) so that both `docker logs`
// and the /logs API work.
var engineLogFile *os.File
if logDir != "" {
serviceLogFile, openErr := os.OpenFile(
filepath.Join(logDir, dmrlogs.ServiceLogName),
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644,
)
if openErr != nil {
log.Warn("failed to open service log file, /logs endpoint will be disabled", "error", openErr)
logDir = ""
} else {
defer serviceLogFile.Close()
bracketW := logging.NewBracketWriter(serviceLogFile)
log = slog.New(slog.NewTextHandler(
io.MultiWriter(os.Stderr, bracketW),
&slog.HandlerOptions{Level: envconfig.LogLevel()},
))
}
if logDir != "" {
var openErr error
engineLogFile, openErr = os.OpenFile(
filepath.Join(logDir, dmrlogs.EngineLogName),
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644,
)
if openErr != nil {
log.Warn("failed to open engine log file", "error", openErr)
} else {
defer engineLogFile.Close()
}
}
}
llamaCppConfig, err := createLlamaCppConfigFromEnv()
if err != nil {
return fmt.Errorf("invalid LLAMA_ARGS: %w", err)
}
svc, err := routing.NewService(routing.ServiceConfig{
Log: log,
ClientConfig: models.ClientConfig{
StoreRootPath: modelPath,
Logger: log.With("component", "model-manager"),
Transport: baseTransport,
RegistryMirrors: envconfig.RegistryMirrors(),
},
Backends: append(
routing.DefaultBackendDefs(routing.BackendsConfig{
Log: log,
ServerLogFactory: func(_ string) logging.Logger {
if engineLogFile == nil {
return log
}
bracketW := logging.NewBracketWriter(engineLogFile)
return slog.New(slog.NewTextHandler(
io.MultiWriter(os.Stderr, bracketW),
&slog.HandlerOptions{Level: envconfig.LogLevel()},
))
},
LlamaCppPath: llamaServerPath,
LlamaCppConfig: llamaCppConfig,
IncludeMLX: true,
MLXPath: mlxServerPath,
IncludeVLLM: includeVLLM,
VLLMPath: vllmServerPath,
VLLMMetalPath: vllmMetalServerPath,
IncludeDiffusers: true,
DiffusersPath: diffusersServerPath,
RegistryMirrors: envconfig.RegistryMirrors(),
}),
routing.BackendDef{Name: sglang.Name, Init: func(mm *models.Manager) (inference.Backend, error) {
return sglang.New(log, mm, log.With("component", sglang.Name), nil, sglangServerPath, nil)
}},
),
OnBackendError: func(name string, err error) {
log.Error("unable to initialize backend", "backend", name, "error", err)
exitFunc(1)
},
DefaultBackendName: llamacpp.Name,
HTTPClient: http.DefaultClient,
MetricsTracker: metrics.NewTracker(
http.DefaultClient,
log.With("component", "metrics"),
"",
false,
),
AllowedOrigins: envconfig.AllowedOrigins(),
IncludeResponsesAPI: true,
ExtraRoutes: func(r *routing.NormalizedServeMux, s *routing.Service) {
// Root handler – only catches exact "/" requests.
r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Docker Model Runner is running"))
})
// Version endpoint.
r.HandleFunc("/version", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
if encErr := json.NewEncoder(w).Encode(map[string]string{"version": cfg.Version}); encErr != nil {
log.Warn("failed to write version response", "error", encErr)
}
})
// Logs endpoint – available when a log directory exists.
if logDir != "" {
r.HandleFunc(
"GET /logs",
dmrlogs.NewHTTPHandler(logDir),
)
log.Info("Logs endpoint enabled at /logs", "dir", logDir)
}
// Metrics endpoint.
if !envconfig.DisableMetrics() {
metricsHandler := metrics.NewAggregatedMetricsHandler(
log.With("component", "metrics"),
s.SchedulerHTTP,
)
r.Handle("/metrics", metricsHandler)
log.Info("Metrics endpoint enabled at /metrics")
} else {
log.Info("Metrics endpoint disabled")
}
},
})
if err != nil {
return fmt.Errorf("failed to initialize service: %w", err)
}
defer svc.Close()
httpServer := &http.Server{
Handler: svc.Router,
ReadHeaderTimeout: 10 * time.Second,
}
serverErrors := make(chan error, 1)
// TLS server (optional).
var tlsServer *http.Server
tlsServerErrors := make(chan error, 1)
// Use TCP port when MODEL_RUNNER_PORT is set; otherwise Unix socket.
tcpPort := envconfig.TCPPort()
if tcpPort != "" {
addr := ":" + tcpPort
log.Info("Listening on TCP port", "port", tcpPort)
httpServer.Addr = addr
go func() {
serverErrors <- httpServer.ListenAndServe()
}()
} else {
if err := os.Remove(sockName); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to remove existing socket: %w", err)
}
}
ln, err := net.ListenUnix("unix", &net.UnixAddr{Name: sockName, Net: "unix"})
if err != nil {
return fmt.Errorf("failed to listen on socket: %w", err)
}
go func() {
serverErrors <- httpServer.Serve(ln)
}()
}
// Start TLS server if enabled.
if envconfig.TLSEnabled() {
tlsPort := envconfig.TLSPort()
certPath := envconfig.TLSCert()
keyPath := envconfig.TLSKey()
if certPath == "" || keyPath == "" {
if envconfig.TLSAutoCert(true) {
log.Info("Auto-generating TLS certificates...")
var err error
certPath, keyPath, err = modeltls.EnsureCertificates("", "")
if err != nil {
return fmt.Errorf("failed to ensure TLS certificates: %w", err)
}
log.Info("Using TLS certificate", "cert", certPath)
log.Info("Using TLS key", "key", keyPath)
} else {
return fmt.Errorf("TLS enabled but no certificate provided and auto-cert is disabled")
}
}
tlsConfig, err := modeltls.LoadTLSConfig(certPath, keyPath)
if err != nil {
return fmt.Errorf("failed to load TLS configuration: %w", err)
}
tlsServer = &http.Server{
Addr: ":" + tlsPort,
Handler: svc.Router,
TLSConfig: tlsConfig,
ReadHeaderTimeout: 10 * time.Second,
}
log.Info("Listening on TLS port", "port", tlsPort)
go func() {
ln, err := tls.Listen("tcp", tlsServer.Addr, tlsConfig)
if err != nil {
tlsServerErrors <- err
return
}
tlsServerErrors <- tlsServer.Serve(ln)
}()
}
schedulerErrors := make(chan error, 1)
go func() {
schedulerErrors <- svc.Scheduler.Run(ctx)
}()
var tlsServerErrorsChan <-chan error
if envconfig.TLSEnabled() {
tlsServerErrorsChan = tlsServerErrors
}
select {
case err := <-serverErrors:
if err != nil {
log.Error("Server error", "error", err)
}
case err := <-tlsServerErrorsChan:
if err != nil {
log.Error("TLS server error", "error", err)
}
case <-ctx.Done():
log.Info("Shutdown signal received")
log.Info("Shutting down the server")
if err := httpServer.Close(); err != nil {
log.Error("Server shutdown error", "error", err)
}
if tlsServer != nil {
log.Info("Shutting down the TLS server")
if err := tlsServer.Close(); err != nil {
log.Error("TLS server shutdown error", "error", err)
}
}
log.Info("Waiting for the scheduler to stop")
if err := <-schedulerErrors; err != nil {
log.Error("Scheduler error", "error", err)
}
}
log.Info("Docker Model Runner stopped")
return nil
}
// createLlamaCppConfigFromEnv builds a LlamaCppConfig from the LLAMA_ARGS
// environment variable. Returns nil config (use defaults) when LLAMA_ARGS is
// unset, or an error if the args contain disallowed flags.
func createLlamaCppConfigFromEnv() (config.BackendConfig, error) {
argsStr := envconfig.LlamaArgs()
if argsStr == "" {
return nil, nil
}
args := splitArgs(argsStr)
disallowedArgs := map[string]struct{}{
"--model": {},
"--host": {},
"--embeddings": {},
"--mmproj": {},
}
for _, arg := range args {
if _, found := disallowedArgs[arg]; found {
return nil, fmt.Errorf("LLAMA_ARGS cannot override %s, which is controlled by the model runner", arg)
}
}
return &llamacpp.Config{Args: args}, nil
}
// splitArgs splits s into arguments, respecting quoted strings.
func splitArgs(s string) []string {
var args []string
var currentArg strings.Builder
inQuotes := false
for _, r := range s {
switch {
case r == '"' || r == '\'':
inQuotes = !inQuotes
case r == ' ' && !inQuotes:
if currentArg.Len() > 0 {
args = append(args, currentArg.String())
currentArg.Reset()
}
default:
currentArg.WriteRune(r)
}
}
if currentArg.Len() > 0 {
args = append(args, currentArg.String())
}
return args
}