forked from cloudwego/abcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
392 lines (323 loc) · 9.85 KB
/
server.go
File metadata and controls
392 lines (323 loc) · 9.85 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
// Copyright 2025 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ipc
import (
"context"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
"github.com/cloudwego/abcoder/lang/java/pb"
"github.com/google/uuid"
)
const (
// DefaultSocketDir is the default directory for Unix Domain Socket files
DefaultSocketDir = "/tmp"
// DefaultConnectTimeout is the timeout for Java process to connect
DefaultConnectTimeout = 30 * time.Second
// DefaultReadTimeout is the timeout for reading individual messages
DefaultReadTimeout = 5 * time.Minute
)
// ServerConfig holds configuration for the Java Parser server
type ServerConfig struct {
// JarPath is the path to the Java Parser JAR file
JarPath string
// JavaHome is the path to Java installation (optional)
JavaHome string
// SocketDir is the directory for Unix Domain Socket files
SocketDir string
// ConnectTimeout is the timeout for Java process to connect
ConnectTimeout time.Duration
// ReadTimeout is the timeout for reading messages
ReadTimeout time.Duration
// Debug enables debug logging
Debug bool
}
// DefaultConfig returns a default server configuration
func DefaultConfig() *ServerConfig {
return &ServerConfig{
SocketDir: DefaultSocketDir,
ConnectTimeout: DefaultConnectTimeout,
ReadTimeout: DefaultReadTimeout,
Debug: false,
}
}
// JavaParserServer manages the Java Parser subprocess and IPC communication
type JavaParserServer struct {
config *ServerConfig
socketPath string
listener *net.UnixListener
javaCmd *exec.Cmd
conn *net.UnixConn
mu sync.Mutex
running bool
stopOnce sync.Once
}
// NewJavaParserServer creates a new Java Parser server with the given configuration
func NewJavaParserServer(config *ServerConfig) *JavaParserServer {
if config == nil {
config = DefaultConfig()
}
return &JavaParserServer{
config: config,
}
}
// Start initializes the UDS listener and starts the Java subprocess.
func (s *JavaParserServer) Start(ctx context.Context, repoPath string, analyzerConfig *pb.AnalyzerConfig) (<-chan *pb.AnalyzeResponse, error) {
s.mu.Lock()
if s.running {
s.mu.Unlock()
return nil, fmt.Errorf("server is already running")
}
s.running = true
s.mu.Unlock()
// Step 1: Create Unix Domain Socket listener
if err := s.createSocketListener(); err != nil {
s.cleanup()
return nil, fmt.Errorf("failed to create socket listener: %w", err)
}
//Step 2: Start Java subprocess
if err := s.startJavaProcess(ctx); err != nil {
s.cleanup()
return nil, fmt.Errorf("failed to start Java process: %w", err)
}
// Step 3: Accept connection from Java process
if err := s.acceptConnection(ctx); err != nil {
s.cleanup()
return nil, fmt.Errorf("failed to accept connection: %w", err)
}
// Step 4: Send analyze request
if err := s.sendAnalyzeRequest(repoPath, analyzerConfig); err != nil {
s.cleanup()
return nil, fmt.Errorf("failed to send analyze request: %w", err)
}
// Step 5: Start reading responses in a goroutine
responseChan := make(chan *pb.AnalyzeResponse, 100)
go s.readResponses(ctx, responseChan)
return responseChan, nil
}
// createSocketListener creates a Unix Domain Socket listener
func (s *JavaParserServer) createSocketListener() error {
// Generate unique socket path
socketName := fmt.Sprintf("java-parser-%s.sock", uuid.New().String()[:8])
s.socketPath = filepath.Join(s.config.SocketDir, socketName)
// Remove existing socket file if present
if err := os.Remove(s.socketPath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove existing socket file: %w", err)
}
// Create Unix Domain Socket listener
addr := &net.UnixAddr{Name: s.socketPath, Net: "unix"}
listener, err := net.ListenUnix("unix", addr)
if err != nil {
return fmt.Errorf("failed to create Unix socket listener: %w", err)
}
s.listener = listener
// Always log socket path
log.Printf("[JavaParserServer] Socket listener created at %s", s.socketPath)
return nil
}
// startJavaProcess starts the Java Parser JAR as a subprocess
func (s *JavaParserServer) startJavaProcess(ctx context.Context) error {
// Determine Java command
javaCmd := "java"
if s.config.JavaHome != "" {
javaCmd = filepath.Join(s.config.JavaHome, "bin", "java")
}
// Build command arguments
args := []string{
"-jar", s.config.JarPath,
"--uds", s.socketPath,
}
// Create command with context for cancellation
s.javaCmd = exec.CommandContext(ctx, javaCmd, args...)
// Redirect stdout and stderr to Go's console for debugging
s.javaCmd.Stdout = os.Stdout
s.javaCmd.Stderr = os.Stderr
// Start the Java process
if err := s.javaCmd.Start(); err != nil {
return fmt.Errorf("failed to start Java process: %w", err)
}
if s.config.Debug {
log.Printf("[JavaParserServer] Java process started with PID %d", s.javaCmd.Process.Pid)
}
// Monitor process in background
go func() {
if err := s.javaCmd.Wait(); err != nil {
if s.config.Debug {
log.Printf("[JavaParserServer] Java process exited: %v", err)
}
}
}()
return nil
}
// acceptConnection waits for the Java process to connect
func (s *JavaParserServer) acceptConnection(ctx context.Context) error {
// Set accept deadline
deadline := time.Now().Add(s.config.ConnectTimeout)
if err := s.listener.SetDeadline(deadline); err != nil {
return fmt.Errorf("failed to set listener deadline: %w", err)
}
// Accept connection
conn, err := s.listener.AcceptUnix()
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
return fmt.Errorf("failed to accept connection: %w", err)
}
}
s.conn = conn
if s.config.Debug {
log.Printf("[JavaParserServer] Connection accepted from Java process")
}
return nil
}
// sendAnalyzeRequest sends the analyze request to the Java process
func (s *JavaParserServer) sendAnalyzeRequest(repoPath string, config *pb.AnalyzerConfig) error {
if config == nil {
config = &pb.AnalyzerConfig{}
}
request := &pb.AnalyzeRequest{
RequestId: uuid.New().String(),
RepoPath: repoPath,
Config: config,
}
writer := NewProtocolWriter(s.conn)
writer.SetDebug(true) // Enable debug logging for sent messages
if err := writer.WriteRequest(request); err != nil {
return fmt.Errorf("failed to write analyze request: %w", err)
}
log.Printf("[JavaParserServer] Analyze request sent for repo: %s", repoPath)
return nil
}
// readResponses reads responses from the Java process and sends them to the channel
func (s *JavaParserServer) readResponses(ctx context.Context, responseChan chan<- *pb.AnalyzeResponse) {
defer close(responseChan)
defer s.cleanup()
reader := NewProtocolReader(s.conn)
reader.SetDebug(true) // Enable debug logging for received messages
for {
select {
case <-ctx.Done():
if s.config.Debug {
log.Printf("[JavaParserServer] Context cancelled, stopping response reader")
}
return
default:
}
// Set read deadline
if s.config.ReadTimeout > 0 {
if err := s.conn.SetReadDeadline(time.Now().Add(s.config.ReadTimeout)); err != nil {
log.Printf("[JavaParserServer] Failed to set read deadline: %v", err)
}
}
// Read next message
outer, err := reader.ReadMessage()
if err != nil {
if err == io.EOF {
if s.config.Debug {
log.Printf("[JavaParserServer] End of stream reached")
}
return
}
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Printf("[JavaParserServer] Read timeout, continuing...")
continue
}
log.Printf("[JavaParserServer] Error reading message: %v", err)
return
}
// 只处理 analyze_response,其它消息(heartbeat/stop 等)忽略
if outer == nil || outer.Type != pb.TYPE_ANALYZE_RESPONSE {
continue
}
resp := outer.GetAnalyzeResponse()
if resp == nil {
continue
}
// Send message to channel
select {
case responseChan <- resp:
if s.config.Debug {
if prog := resp.GetProgress(); prog != nil {
log.Printf("[JavaParserServer] Progress: %d%% - %s", prog.Percentage, prog.Message)
}
}
// Check for summary (indicates completion)
if sum := resp.GetSummary(); sum != nil {
if s.config.Debug {
log.Printf("[JavaParserServer] Analysis complete: %d classes, %d files in %dms",
sum.LocalClassCount, sum.FileCount, sum.TotalTimeMs)
}
return
}
// Check for errors
if errInfo := resp.GetError(); errInfo != nil {
code := string(errInfo.Code)
if code == "" {
code = "unknown"
}
log.Printf("[JavaParserServer] Error from Java parser: %s - %s", code, errInfo.Message)
}
case <-ctx.Done():
return
}
}
}
// Stop gracefully stops the server and cleans up resources
func (s *JavaParserServer) Stop() {
s.stopOnce.Do(func() {
s.cleanup()
})
}
// cleanup releases all resources
func (s *JavaParserServer) cleanup() {
s.mu.Lock()
defer s.mu.Unlock()
if s.conn != nil {
s.conn.Close()
s.conn = nil
}
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
if s.socketPath != "" {
os.Remove(s.socketPath)
}
if s.javaCmd != nil && s.javaCmd.Process != nil {
s.javaCmd.Process.Kill()
s.javaCmd = nil
}
s.running = false
if s.config.Debug {
log.Printf("[JavaParserServer] Cleanup complete")
}
}
// GetSocketPath returns the current socket path
func (s *JavaParserServer) GetSocketPath() string {
return s.socketPath
}
// IsRunning returns whether the server is currently running
func (s *JavaParserServer) IsRunning() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.running
}