-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RONDB-858: Use go avro lib in RDRS2 as cpp lib is slow
- Loading branch information
Showing
24 changed files
with
601 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON) | |
|
||
add_subdirectory(src) | ||
add_subdirectory(test) | ||
add_subdirectory(avro) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
project(libavro) | ||
|
||
set(SHARED_LIB "${CMAKE_BINARY_DIR}/library_output_directory/libavro.a") | ||
|
||
# Build the Go library | ||
|
||
|
||
#COMMAND go build -ldflags=-extldflags=-Wl,-rpath,"${CMAKE_CURRENT_BINARY_DIR}/lib" -o ${SHARED_LIB} -buildmode=c-shared avro.go | ||
add_custom_target(LIBAVRO_BUILD ALL | ||
COMMAND ${CMAKE_COMMAND} -E env CGO_LDFLAGS="-O3" go build -o ${SHARED_LIB} -buildmode=c-archive avro.go avro_parser.go | ||
COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include | ||
COMMAND mv ${CMAKE_BINARY_DIR}/library_output_directory/libavro.h ${CMAKE_CURRENT_BINARY_DIR}/include/avro.h | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
COMMENT "Building libavro (Go CGO shared library)" | ||
) | ||
|
||
# Ensure the headers and libraries are available | ||
add_library(libavro SHARED IMPORTED) | ||
set_target_properties(libavro PROPERTIES | ||
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${SHARED_LIB}" | ||
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/include" | ||
) | ||
|
||
# Ensure the library is built before the main project uses it | ||
add_dependencies(libavro LIBAVRO_BUILD) | ||
|
||
# Set PATHS | ||
SET(RDRS_LIBAVRO_LIB_DIR "${CMAKE_BINARY_DIR}/library_output_directory" CACHE INTERNAL "RDRS libavro dir" FORCE) | ||
SET(RDRS_LIBAVRO_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include" CACHE INTERNAL "RDRS libavro include dir" FORCE) | ||
|
||
# Install step | ||
INSTALL(FILES "${SHARED_LIB}" DESTINATION lib | ||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE | ||
GROUP_READ GROUP_EXECUTE | ||
WORLD_READ WORLD_EXECUTE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2023, 2025, Hopsworks and/or its affiliates. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | ||
* USA. | ||
*/ | ||
|
||
package main | ||
|
||
/* | ||
#include <string.h> | ||
#include <stdint.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"sync/atomic" | ||
"unsafe" | ||
|
||
"github.com/bytedance/sonic" | ||
"github.com/hamba/avro/v2" | ||
) | ||
|
||
var curSchemaID atomic.Int64 | ||
|
||
type ComplexFeature struct { | ||
schemaStr string | ||
AvroSchema *avro.Schema | ||
AvroStruct *reflect.Type | ||
} | ||
|
||
var avroStructs = make(map[int64]*ComplexFeature) | ||
|
||
//export register_schema | ||
func register_schema(schema string) C.int64_t { | ||
|
||
avroSchema, err := avro.Parse(string(schema)) | ||
if err != nil { | ||
//fmt.Fprintf(os.Stderr, "Failed to parse avro schemd %s. Error: %v\n", schema, err) | ||
return -1 | ||
} | ||
|
||
avroStruct, err := ConvertAvroSchemaToStruct(avroSchema) | ||
if err != nil { | ||
//fmt.Fprintf(os.Stderr, "Failed to generate strcut for avro schemd %s. Error: %v\n", schema, err) | ||
return -1 | ||
} | ||
id := curSchemaID.Add(1) | ||
avroStructs[id] = &ComplexFeature{schemaStr: schema, AvroSchema: &avroSchema, AvroStruct: &avroStruct} | ||
|
||
//fmt.Printf("Go lang. Registered schema: %s. ID: %d\n", schema, id) | ||
return C.int64_t(id) | ||
} | ||
|
||
//export unregister_schema | ||
func unregister_schema(schema_id C.int64_t) { | ||
delete(avroStructs, int64(schema_id)) | ||
//fmt.Printf("Go lang. Deleted schema ID: %d\n", schema_id) | ||
} | ||
|
||
//export unmarshal_avro | ||
func unmarshal_avro(schema_id C.int64_t, data []byte, outStr **C.char, outLen *C.int32_t) C.int64_t { | ||
//fmt.Printf("Go lang. Unmarshal: Schema ID: %d\n", schema_id) | ||
|
||
// var avroDeserialized interface{} | ||
cf, ok := avroStructs[int64(schema_id)] | ||
if !ok { | ||
//fmt.Fprintf(os.Stderr, "Failed to unmarshall avro data. Schema ID: %d not found \n", schema_id) | ||
return C.int64_t(-1) | ||
} | ||
|
||
avroDeserialized := reflect.New(*cf.AvroStruct).Interface() | ||
err := avro.Unmarshal(*cf.AvroSchema, data, &avroDeserialized) | ||
if err != nil { | ||
//fmt.Fprintf(os.Stderr, "Failed to unmarshall avro data. Schema ID: %d. Error: %v\n", schema_id, err) | ||
return C.int64_t(-1) | ||
} | ||
|
||
// dicsard the top most wrapper | ||
j := reflect.ValueOf(avroDeserialized).Elem().Field(0).Interface() | ||
|
||
bytes, err := sonic.Marshal(j) | ||
if err != nil { | ||
//fmt.Fprintf(os.Stderr, "Failed to unmarshall avro data. Schema ID: %d. Error: %v\n", schema_id, err) | ||
return C.int64_t(-1) | ||
} | ||
|
||
heapStr := C.malloc(C.size_t(len(bytes))) | ||
if heapStr == nil { | ||
fmt.Fprintln(os.Stderr, "Failed to allocate memory\n") | ||
return C.int64_t(-1) | ||
} | ||
|
||
// Copy Go slice to C heap memory | ||
C.memcpy(heapStr, unsafe.Pointer(&bytes[0]), C.size_t(len(bytes))) | ||
|
||
*outStr = (*C.char)(heapStr) | ||
*outLen = C.int32_t(len(bytes)) | ||
|
||
return C.int64_t(0) | ||
} | ||
|
||
func main() {} |
Oops, something went wrong.