github.com/apache/beam/sdks/v2@v2.48.2/go/test/integration/io/xlang/kafka/jar.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one or more 2 // contributor license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright ownership. 4 // The ASF licenses this file to You under the Apache License, Version 2.0 5 // (the "License"); you may not use this file except in compliance with 6 // the License. You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package kafka 17 18 import ( 19 "fmt" 20 "net" 21 "os/exec" 22 "strconv" 23 "time" 24 ) 25 26 // runLocalKafka takes a Kafka jar filepath and runs a local Kafka cluster with 27 // a timeout (via the timeout command), returning the bootstrap server. Requires 28 // an environment with the timeout command. 29 func runLocalKafka(jar string, timeout string) (*cluster, error) { 30 _, err := exec.LookPath("java") 31 if err != nil { 32 return nil, err 33 } 34 _, err = exec.LookPath("timeout") 35 if err != nil && len(timeout) != 0 { 36 return nil, fmt.Errorf("\"timeout\" required for kafka_jar_timeout flag: %s", err) 37 } 38 39 port, err := getOpenPort() 40 if err != nil { 41 return nil, err 42 } 43 kafkaPort := strconv.Itoa(port) 44 port, err = getOpenPort() 45 if err != nil { 46 return nil, err 47 } 48 zookeeperPort := strconv.Itoa(port) 49 50 var cmdArr []string 51 if len(timeout) != 0 { 52 cmdArr = append(cmdArr, "timeout", timeout) 53 } 54 cmdArr = append(cmdArr, "java", "-jar", jar, kafkaPort, zookeeperPort) 55 56 cmd := exec.Command(cmdArr[0], cmdArr[1:]...) 57 err = cmd.Start() 58 if err != nil { 59 return nil, err 60 } 61 time.Sleep(3 * time.Second) // Wait a bit for the cluster to start. 62 63 return &cluster{proc: cmd.Process, bootstrapAddr: "localhost:" + kafkaPort}, nil 64 } 65 66 // getOpenPort gets an open TCP port and returns it, or an error on failure. 67 func getOpenPort() (int, error) { 68 listener, err := net.Listen("tcp", ":0") 69 if err != nil { 70 return 0, err 71 } 72 defer listener.Close() 73 return listener.Addr().(*net.TCPAddr).Port, nil 74 }