This is a sample GRPC server that provides a gateway to Pravega. It provides limited Pravega functionality to any environment that support GRPC, including Python.
Using a GRPC gateway is better than a REST gateway for the following reasons:
-
GRPC streaming is used for reading and writing events. This allows the Pravega connection to remain open for the life of the streaming request. Within a streaming request, any number of read or write operations can be performed. In the case of writing, these can also be wrapped in transactions. Events can be marked to commit the current transaction and open a new one.
-
GRPC uses Protobuf for efficient serialization. REST/JSON requires base64 encoding of binary data which is less efficient.
-
GRPC stubs (client code) can be easily created for nearly any language and environment.
-
Make sure to have
JDK-8
&JRE-8
in your system. -
Save Pravega Controller Running address in environment. Change
localhost
and9090
according to your setup.export PRAVEGA_CONTROLLER=tcp://localhost:9090
-
Run
gradlew
file../gradlew run
-
Pravega gRPC Gateway would be running at Port
54672
.
-
Save Pravega Controller Running address in environment. Change
localhost
and9090
according to your setup.export PRAVEGA_CONTROLLER=tcp://localhost:9090
-
Run the following commands to have Pravega gRPC Gateway Up & Running at Port
54672
.export DOCKER_REPOSITORY=claudiofahey export IMAGE_TAG=0.7.0 docker run -d \ --restart always \ -e PRAVEGA_CONTROLLER \ -p 54672:80 \ --name pravega-grpc-gateway \ ${DOCKER_REPOSITORY}/pravega-grpc-gateway:${IMAGE_TAG}
-
Edit the file
charts/pravega-grpc-gateway/values.yaml
as needed. You will need to setpravega.controller
andservice.annotations.external-dns.alpha.kubernetes.io/hostname
to match your environment. -
Build and then deploy using Helm.
export DOCKER_REPOSITORY=claudiofahey export IMAGE_TAG=0.7.0 export NAMESPACE=examples scripts/build-k8s-components.sh scripts/deploy-k8s-components.sh
-
Create & Enabling Virtual Enivornment.
python3 -m venv venv source venv/bin/activate
-
Run to following command to install Pravega gRPC Client in your environment.
git clone https://github.com/pravega/pravega-grpc-gateway /tmp/pravega-grpc-gateway && \ cd /tmp/pravega-grpc-gateway && \ pip install grpcio pravega-grpc-gateway/src/main/python
-
Run integration_test1.py for confirming the Setup & Connection. Do Change Pravega gRPC Gateway IP & Port in integration_test1.py, according to your setup.
-
Install Miniconda Python 3.7.
-
Create Conda environment.
git clone https://github.com/pravega/pravega-grpc-gateway cd pravega-grpc-gateway/pravega-grpc-gateway/ ./create_conda_env.sh
-
Activate Conda Environemnt.
conda activate ./env
-
Install Pravega gRPC Gateway Client.
pip install -e src/main/python
-
Run integration_test1.py for confirming the Setup & Connection. Do Change Pravega gRPC Gateway IP & Port in integration_test1.py, according to your setup.
Replace pravega-grpc-gateway.example.com:80
with your Host IP & Port of Pravega gRPC Gateway.
import grpc
import pravega.grpc_gateway as pravega
gateway = 'pravega-grpc-gateway.example.com:80'
pravega_channel = grpc.insecure_channel(gateway, options=[
('grpc.max_receive_message_length', 9*1024*1024),
])
pravega_client = pravega.grpc.PravegaGatewayStub(pravega_channel)
events_to_write = (pravega.pb.WriteEventsRequest(
scope='examples',
stream='my-stream',
event=('%d,%s' % (i, datetime.datetime.now())).encode('UTF-8'),
routing_key='0',
) for i in range(10))
pravega_client.WriteEvents(events_to_write)
read_events_request = pravega.pb.ReadEventsRequest(
scope='examples',
stream='my-stream',
from_stream_cut=None,
to_stream_cut=None,
)
for event in pravega_client.ReadEvents(read_events_request):
print(event.event.args.decode('UTF-8'))
-
event_generator.py contains example for writing into stream.
-
event_reader.py contains example for reading from stream event by event.
-
batch_reader.py contains example for reading stream into batches.
-
integration_test1.py contains bunch of implementation (eg. Truncating & Deleting Stream).
-
See pravega.proto for all available RPCs.
This section is only needed if you make changes to the pravega.proto file.
This will build the Python files necessary to allow a Python application to call this gateway.
-
Create Python environment as shown above.
-
Run Protobuf compiler.
./build_python.sh