generated from steadybit/extension-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java_extension_facade.go
459 lines (407 loc) · 13.3 KB
/
java_extension_facade.go
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package extjvm
import (
"bufio"
"errors"
"fmt"
"github.com/dimchansky/utfbom"
"github.com/rs/zerolog/log"
"github.com/steadybit/extension-jvm/extjvm/attachment"
"github.com/steadybit/extension-jvm/extjvm/attachment/plugin_tracking"
"github.com/steadybit/extension-jvm/extjvm/attachment/remote_jvm_connections"
"github.com/steadybit/extension-jvm/extjvm/common"
"github.com/steadybit/extension-jvm/extjvm/java_process"
"github.com/steadybit/extension-jvm/extjvm/jvm"
"github.com/steadybit/extension-kit/extutil"
"io"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type JavaExtensionFacade struct{}
type AttachJvmWork struct {
jvm *jvm.JavaVm
retries int
}
type LoadPluginJvmWork struct {
jvm *jvm.JavaVm
plugin string
args string
retries int
}
type AutoloadPlugin struct {
MarkerClass string
Plugin string
}
type AttachedListener interface {
JvmAttachedSuccessfully(jvm *jvm.JavaVm)
AttachedProcessStopped(jvm *jvm.JavaVm)
}
var (
attachJobs = make(chan AttachJvmWork)
loadPluginJobs = make(chan LoadPluginJvmWork)
autoloadPlugins = make([]AutoloadPlugin, 0)
autoloadPluginsMutex sync.Mutex
JavaagentInitJar = common.GetJarPath("javaagent-init.jar")
JavaagentMainJar = common.GetJarPath("javaagent-main.jar")
attachedListeners []AttachedListener
SocketTimeout = 10 * time.Second
)
func StartAttachment() {
attachmentEnabled := os.Getenv("STEADYBIT_EXTENSION_JVM_ATTACHMENT_ENABLED")
if attachmentEnabled != "" && strings.ToLower(attachmentEnabled) != "true" {
return
}
// create attach worker pool
for w := 1; w <= 4; w++ {
go attachWorker(attachJobs)
}
// create plugin load worker pool
for w := 1; w <= 4; w++ {
go loadPluginWorker(loadPluginJobs)
}
AddListener(&JavaExtensionFacade{})
}
func AddAttachedListener(attachedListener AttachedListener) {
attachedListeners = append(attachedListeners, attachedListener)
for _, jvm := range GetJVMs() {
attachedListener.JvmAttachedSuccessfully(&jvm)
}
}
func attachWorker(attachJobs chan AttachJvmWork) {
for job := range attachJobs {
job.retries--
if job.retries > 0 {
doAttach(job)
} else {
log.Warn().Msgf("Attach retries for %s exceeded.", job.jvm.ToDebugString())
}
}
}
func loadPluginWorker(loadPluginJobs chan LoadPluginJvmWork) {
for job := range loadPluginJobs {
job.retries--
if job.retries > 0 {
loadAgentPluginJob(job)
} else {
log.Warn().Msgf("Load Plugin retries for %s with plugin %s exceeded.", job.jvm.MainClass, job.plugin)
}
}
}
func doAttach(job AttachJvmWork) {
vm := job.jvm
ok, err := attachInternal(vm)
if err != nil {
if java_process.IsRunningProcess(vm.Pid) {
log.Warn().Msgf("Error attaching to JVM %+v: %s", vm, err)
go func() {
time.Sleep(10 * time.Second)
// do retry
attach(job.jvm, job.retries)
}()
} else {
log.Trace().Msgf("Jvm stopped, attach failed. JVM %+v: %s", vm, err)
}
return
}
if ok {
log.Info().Msgf("Successful attachment to JVM %+v", vm)
loglevel := getJvmExtensionLogLevel()
log.Trace().Msgf("Propagating Loglevel %s to Javaagent in JVM %+v", loglevel, vm)
if !setLogLevel(vm, loglevel) {
//If setting the loglevel fails, the connection has some issue - do retry
attach(job.jvm, job.retries)
} else {
log.Info().Msgf("Successfully set loglevel %s for JVM %+v", loglevel, vm)
}
for _, plugin := range autoloadPlugins {
loadAutoLoadPlugin(vm, plugin.MarkerClass, plugin.Plugin)
}
informListeners(vm)
} else {
log.Debug().Msgf("Attach to JVM skipped. Excluding %+v", vm)
}
}
func informListeners(vm *jvm.JavaVm) {
for _, listener := range attachedListeners {
listener := listener
go func() {
listener.JvmAttachedSuccessfully(vm)
}()
}
}
func scheduleLoadAgentPlugin(jvm *jvm.JavaVm, plugin string, args string, retries int) {
loadPluginJobs <- LoadPluginJvmWork{
jvm: jvm,
plugin: plugin,
args: args,
retries: retries,
}
}
func loadAgentPluginJob(job LoadPluginJvmWork) {
success, err := LoadAgentPlugin(job.jvm, job.plugin, job.args)
if err != nil || !success {
log.Error().Msgf("Error loading plugin %s for JVM %+v: %s", job.plugin, job.jvm, err)
go func() {
time.Sleep(10 * time.Second)
// do retry
scheduleLoadAgentPlugin(job.jvm, job.plugin, job.args, job.retries)
}()
return
}
}
func LoadAgentPlugin(jvm *jvm.JavaVm, plugin string, args string) (bool, error) {
if HasAgentPlugin(jvm, plugin) {
log.Trace().Msgf("Plugin %s already loaded for JVM %+v", plugin, jvm)
return true, nil
}
_, err := os.Stat(plugin)
if err != nil {
log.Error().Msgf("Plugin %s not found: %s", plugin, err)
return false, err
}
var pluginPath string
if jvm.IsRunningInContainer() {
file := filepath.Base(plugin)
file = fmt.Sprintf("steadybit-%s", file)
files, err := attachment.GetAttachment(jvm).CopyFiles("/tmp", map[string]string{
file: plugin,
})
if err != nil {
log.Error().Msgf("Error copying plugin %s to container: %s", plugin, err)
return false, err
}
pluginPath = files[file]
} else {
pluginPath = plugin
}
loaded := sendCommandToAgent(jvm, "load-agent-plugin", fmt.Sprintf("%s,%s", pluginPath, args), time.Duration(30)*time.Second)
if loaded {
log.Info().Msgf("Plugin %s loaded for JVM %+s", plugin, jvm.ToInfoString())
plugin_tracking.Add(jvm.Pid, plugin)
return true, nil
}
log.Warn().Msgf("Plugin %s not loaded for JVM %+v", plugin, jvm)
return false, nil
}
func unloadAutoLoadPlugin(jvm *jvm.JavaVm, markerClass string, plugin string) {
if HasClassLoaded(jvm, markerClass) {
log.Debug().Msgf("Unloading plugin %s for JVM %+v", plugin, jvm)
_, err := UnloadAgentPlugin(jvm, plugin)
if err != nil {
log.Warn().Msgf("Unloading plugin %s for JVM %+v failed: %s", plugin, jvm, err)
}
}
}
func UnloadAgentPlugin(jvm *jvm.JavaVm, plugin string) (bool, error) {
_, err := os.Stat(plugin)
if err != nil {
log.Error().Msgf("Plugin %s not found: %s", plugin, err)
return false, err
}
var args string
if jvm.IsRunningInContainer() {
file := filepath.Base(plugin)
file = fmt.Sprintf("steadybit-%s", file)
args = "/tmp/" + file + ",deleteFile=true"
} else {
args = plugin
}
unloaded := SendCommandToAgent(jvm, "unload-agent-plugin", args)
if unloaded {
plugin_tracking.Remove(jvm.Pid, plugin)
}
return unloaded, nil
}
func HasAgentPlugin(jvm *jvm.JavaVm, plugin string) bool {
return plugin_tracking.Has(jvm.Pid, plugin)
}
func HasClassLoaded(jvm *jvm.JavaVm, className string) bool {
return SendCommandToAgent(jvm, "class-loaded", className)
}
func setLogLevel(jvm *jvm.JavaVm, loglevel string) bool {
return SendCommandToAgent(jvm, "log-level", loglevel)
}
func SendCommandToAgent(jvm *jvm.JavaVm, command string, args string) bool {
return sendCommandToAgent(jvm, command, args, SocketTimeout)
}
func sendCommandToAgent(jvm *jvm.JavaVm, command string, args string, timeout time.Duration) bool {
log.Debug().Msgf("Sending command %s:%s to agent on PID %d", command, args, jvm.Pid)
success := sendCommandToAgentViaSocket(jvm, command, args, timeout, func(rc string, response io.Reader) bool {
resultMessage, err := GetCleanSocketCommandResult(response)
log.Debug().Msgf("Result from command %s:%s agent on PID %d: %s", command, args, jvm.Pid, resultMessage)
if err != nil {
log.Error().Msgf("Error reading result from command %s:%s agent on PID %d: %s", command, args, jvm.Pid, err)
return false
}
if resultMessage != "" && rc == "OK" {
log.Debug().Msgf("Command '%s:%s' to agent on PID %d returned %s", command, args, jvm.Pid, resultMessage)
return extutil.ToBool(resultMessage)
} else {
log.Warn().Msgf("Command '%s:%s' to agent on PID %d returned error: %s", command, args, jvm.Pid, resultMessage)
return false
}
})
return success != nil && *success
}
func SendCommandToAgentViaSocket[T any](jvm *jvm.JavaVm, command string, args string, handler func(rc string, response io.Reader) T) *T {
return sendCommandToAgentViaSocket(jvm, command, args, SocketTimeout, handler)
}
func sendCommandToAgentViaSocket[T any](jvm *jvm.JavaVm, command string, args string, timeout time.Duration, handler func(rc string, response io.Reader) T) *T {
pid := jvm.Pid
connection := remote_jvm_connections.GetConnection(pid)
if connection == nil {
log.Debug().Msgf("RemoteJvmConnection from PID %d not found. Command '%s:%s' not sent.", pid, command, args)
return nil
}
connection.Mutex.Lock()
defer connection.Mutex.Unlock()
d := net.Dialer{Timeout: timeout}
conn, err := d.Dial("tcp", connection.Address())
defer func(conn net.Conn) {
if conn == nil {
return
}
err := conn.Close()
if err != nil {
log.Error().Msgf("Error closing socket connection to JVM %d: %s", pid, err)
}
}(conn)
if err != nil {
if java_process.IsRunningProcess(pid) {
log.Error().Msgf("Command '%s' could not be sent over socket to %+v (%s): %s", command, jvm, connection.Address(), err)
} else {
log.Debug().Msgf("Process %d not running anymore. Removing connection to %+v:%s", pid, jvm, connection.Address())
remote_jvm_connections.RemoveConnection(pid)
}
return nil
}
err = conn.SetDeadline(time.Now().Add(timeout))
if err != nil {
log.Error().Msgf("Error setting deadline for connection to JVM %d: %s", pid, err)
return nil
}
err = conn.SetWriteDeadline(time.Now().Add(timeout))
if err != nil {
log.Error().Msgf("Error setting write deadline for connection to JVM %d: %s", pid, err)
return nil
}
err = conn.SetReadDeadline(time.Now().Add(timeout))
if err != nil {
log.Error().Msgf("Error setting read deadline for connection to JVM %d: %s", pid, err)
return nil
}
log.Debug().Msgf("Sending command '%s:%s' to agent on PID %d", command, args, pid)
//_, err = conn.Write([]byte(command + ":" + args + "\n"))
// Commands must end with newline
_, err = fmt.Fprintf(conn, "%s:%s\n", command, args)
if err != nil {
log.Error().Msgf("Error sending command '%s:%s' to JVM %d: %s", command, args, pid, err)
return nil
}
// First byte is always the return code
rcByte := make([]byte, 1)
_, err = conn.Read(rcByte)
if err != nil {
log.Error().Msgf("Error reading response return code from JVM %d: %s", pid, err)
return nil
}
var rc string
if rcByte[0] == 0 {
rc = "OK"
} else if rcByte[0] == 1 {
rc = "ERROR"
} else {
rc = "UNKNOWN"
}
log.Debug().Msgf("Return code from JVM %s for command %s:%s on pid %d", rc, command, args, pid)
return extutil.Ptr(handler(rc, utfbom.SkipOnly(conn)))
}
func GetCleanSocketCommandResult(response io.Reader) (string, error) {
resultMessage, err := bufio.NewReader(response).ReadString('\n')
if err != nil {
return "", err
}
message := strings.Trim(resultMessage, "\n")
return message, nil
}
func getJvmExtensionLogLevel() string {
loglevel := os.Getenv("STEADYBIT_EXTENSION_LOG_LEVEL")
if loglevel == "" {
loglevel = "info"
}
return strings.ToUpper(loglevel)
}
func attachInternal(jvm *jvm.JavaVm) (bool, error) {
if isAttached(jvm) {
log.Trace().Msgf("RemoteJvmConnection to JVM already established. %+v", jvm)
return true, nil
}
log.Debug().Msgf("RemoteJvmConnection to JVM not found. Attaching now. %+v", jvm)
if _, err := os.Stat(JavaagentMainJar); err != nil {
log.Error().Msgf("JavaagentMainJar not found: %s", JavaagentMainJar)
return false, err
}
if _, err := os.Stat(JavaagentInitJar); err != nil {
log.Error().Msgf("JavaagentInitJar not found: %s", JavaagentInitJar)
return false, err
}
attached := attachment.GetAttachment(jvm).Attach(JavaagentMainJar, JavaagentInitJar, int(common.GetOwnJVMAttachmentPort()))
if !attached {
return false, errors.New("could not attach to JVM")
}
jvmConnectionPresent := remote_jvm_connections.WaitForConnection(jvm.Pid, time.Duration(90)*time.Second)
if !jvmConnectionPresent {
log.Error().Msgf("JVM with did not call back after 90 seconds.")
return false, errors.New("could not attach to JVM: VM with did not call back after 90 seconds")
}
return true, nil
}
func isAttached(jvm *jvm.JavaVm) bool {
connection := remote_jvm_connections.GetConnection(jvm.Pid)
return connection != nil
}
func (j JavaExtensionFacade) AddedJvm(jvm *jvm.JavaVm) {
attach(jvm, 5)
}
func attach(jvm *jvm.JavaVm, retries int) {
attachJobs <- AttachJvmWork{jvm: jvm, retries: retries}
}
func (j JavaExtensionFacade) RemovedJvm(jvm *jvm.JavaVm) {
plugin_tracking.RemoveAll(jvm.Pid)
for _, listener := range attachedListeners {
listener.AttachedProcessStopped(jvm)
}
}
func AddAutoloadAgentPlugin(plugin string, markerClass string) {
autoloadPluginsMutex.Lock()
autoloadPlugins = append(autoloadPlugins, AutoloadPlugin{Plugin: plugin, MarkerClass: markerClass})
autoloadPluginsMutex.Unlock()
vms := GetJVMs()
for _, vm := range vms {
loadAutoLoadPlugin(&vm, markerClass, plugin)
}
}
func loadAutoLoadPlugin(jvm *jvm.JavaVm, markerClass string, plugin string) {
log.Info().Msgf("Autoloading plugin %s for %s", plugin, jvm.ToDebugString())
if HasClassLoaded(jvm, markerClass) {
log.Info().Msgf("Sending plugin %s for %s: %s", plugin, jvm.ToDebugString(), markerClass)
scheduleLoadAgentPlugin(jvm, plugin, "", 5)
}
}
func RemoveAutoloadAgentPlugin(plugin string, markerClass string) {
for i, p := range autoloadPlugins {
if p.Plugin == plugin && p.MarkerClass == markerClass {
autoloadPluginsMutex.Lock()
autoloadPlugins = append(autoloadPlugins[:i], autoloadPlugins[i+1:]...)
autoloadPluginsMutex.Unlock()
break
}
}
jvMs := GetJVMs()
for _, vm := range jvMs {
unloadAutoLoadPlugin(&vm, plugin, markerClass)
}
}