gorealis v2 refactor (#5)

* Changing default timeout for start maintenance.

* Upgrading dependencies to gorealis v2 and thrift  0.12.0

* Refactored to update to gorealis v2.
This commit is contained in:
Renan DelValle 2018-12-27 11:31:51 -08:00 committed by GitHub
parent ad4dd9606e
commit 6ab5c9334d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1335 changed files with 137431 additions and 61530 deletions

View file

@ -0,0 +1,130 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.
#[macro_use]
extern crate clap;
extern crate thrift;
extern crate thrift_tutorial;
use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol};
use thrift::transport::{
ReadHalf, TFramedReadTransport, TFramedWriteTransport, TIoChannel, TTcpChannel, WriteHalf,
};
use thrift_tutorial::shared::TSharedServiceSyncClient;
use thrift_tutorial::tutorial::{CalculatorSyncClient, Operation, TCalculatorSyncClient, Work};
fn main() {
match run() {
Ok(()) => println!("tutorial client ran successfully"),
Err(e) => {
println!("tutorial client failed with error {:?}", e);
std::process::exit(1);
}
}
}
fn run() -> thrift::Result<()> {
let options = clap_app!(rust_tutorial_client =>
(version: "0.1.0")
(author: "Apache Thrift Developers <dev@thrift.apache.org>")
(about: "Thrift Rust tutorial client")
(@arg host: --host +takes_value "host on which the tutorial server listens")
(@arg port: --port +takes_value "port on which the tutorial server listens")
);
let matches = options.get_matches();
// get any passed-in args or the defaults
let host = matches.value_of("host").unwrap_or("127.0.0.1");
let port = value_t!(matches, "port", u16).unwrap_or(9090);
// build our client and connect to the host:port
let mut client = new_client(host, port)?;
// alright!
// let's start making some calls
// let's start with a ping; the server should respond
println!("ping!");
client.ping()?;
// simple add
println!("add");
let res = client.add(1, 2)?;
println!("added 1, 2 and got {}", res);
let logid = 32;
// let's do...a multiply!
let res = client.calculate(logid, Work::new(7, 8, Operation::Multiply, None))?;
println!("multiplied 7 and 8 and got {}", res);
// let's get the log for it
let res = client.get_struct(32)?;
println!("got log {:?} for operation {}", res, logid);
// ok - let's be bad :(
// do a divide by 0
// logid doesn't matter; won't be recorded
let res = client.calculate(77, Work::new(2, 0, Operation::Divide, "we bad".to_owned()));
// we should have gotten an exception back
match res {
Ok(v) => panic!("should not have succeeded with result {}", v),
Err(e) => println!("divide by zero failed with error {:?}", e),
}
// let's do a one-way call
println!("zip");
client.zip()?;
// and then close out with a final ping
println!("ping!");
client.ping()?;
Ok(())
}
type ClientInputProtocol = TCompactInputProtocol<TFramedReadTransport<ReadHalf<TTcpChannel>>>;
type ClientOutputProtocol = TCompactOutputProtocol<TFramedWriteTransport<WriteHalf<TTcpChannel>>>;
fn new_client(
host: &str,
port: u16,
) -> thrift::Result<CalculatorSyncClient<ClientInputProtocol, ClientOutputProtocol>> {
let mut c = TTcpChannel::new();
// open the underlying TCP stream
println!("connecting to tutorial server on {}:{}", host, port);
c.open(&format!("{}:{}", host, port))?;
// clone the TCP channel into two halves, one which
// we'll use for reading, the other for writing
let (i_chan, o_chan) = c.split()?;
// wrap the raw sockets (slow) with a buffered transport of some kind
let i_tran = TFramedReadTransport::new(i_chan);
let o_tran = TFramedWriteTransport::new(o_chan);
// now create the protocol implementations
let i_prot = TCompactInputProtocol::new(i_tran);
let o_prot = TCompactOutputProtocol::new(o_tran);
// we're done!
Ok(CalculatorSyncClient::new(i_prot, o_prot))
}

View file

@ -0,0 +1,179 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.
#[macro_use]
extern crate clap;
extern crate thrift;
extern crate thrift_tutorial;
use std::collections::HashMap;
use std::convert::{From, Into};
use std::default::Default;
use std::sync::Mutex;
use thrift::protocol::{TCompactInputProtocolFactory, TCompactOutputProtocolFactory};
use thrift::server::TServer;
use thrift::transport::{TFramedReadTransportFactory, TFramedWriteTransportFactory};
use thrift_tutorial::shared::{SharedServiceSyncHandler, SharedStruct};
use thrift_tutorial::tutorial::{CalculatorSyncHandler, CalculatorSyncProcessor};
use thrift_tutorial::tutorial::{InvalidOperation, Operation, Work};
fn main() {
match run() {
Ok(()) => println!("tutorial server ran successfully"),
Err(e) => {
println!("tutorial server failed with error {:?}", e);
std::process::exit(1);
}
}
}
fn run() -> thrift::Result<()> {
let options = clap_app!(rust_tutorial_server =>
(version: "0.1.0")
(author: "Apache Thrift Developers <dev@thrift.apache.org>")
(about: "Thrift Rust tutorial server")
(@arg port: --port +takes_value "port on which the tutorial server listens")
);
let matches = options.get_matches();
let port = value_t!(matches, "port", u16).unwrap_or(9090);
let listen_address = format!("127.0.0.1:{}", port);
println!("binding to {}", listen_address);
let i_tran_fact = TFramedReadTransportFactory::new();
let i_prot_fact = TCompactInputProtocolFactory::new();
let o_tran_fact = TFramedWriteTransportFactory::new();
let o_prot_fact = TCompactOutputProtocolFactory::new();
// demux incoming messages
let processor = CalculatorSyncProcessor::new(CalculatorServer {
..Default::default()
});
// create the server and start listening
let mut server = TServer::new(
i_tran_fact,
i_prot_fact,
o_tran_fact,
o_prot_fact,
processor,
10,
);
server.listen(&listen_address)
}
/// Handles incoming Calculator service calls.
struct CalculatorServer {
log: Mutex<HashMap<i32, SharedStruct>>,
}
impl Default for CalculatorServer {
fn default() -> CalculatorServer {
CalculatorServer {
log: Mutex::new(HashMap::new()),
}
}
}
// since Calculator extends SharedService we have to implement the
// handler for both traits.
//
// SharedService handler
impl SharedServiceSyncHandler for CalculatorServer {
fn handle_get_struct(&self, key: i32) -> thrift::Result<SharedStruct> {
let log = self.log.lock().unwrap();
log.get(&key)
.cloned()
.ok_or_else(|| format!("could not find log for key {}", key).into())
}
}
// Calculator handler
impl CalculatorSyncHandler for CalculatorServer {
fn handle_ping(&self) -> thrift::Result<()> {
println!("pong!");
Ok(())
}
fn handle_add(&self, num1: i32, num2: i32) -> thrift::Result<i32> {
println!("handling add: n1:{} n2:{}", num1, num2);
Ok(num1 + num2)
}
fn handle_calculate(&self, logid: i32, w: Work) -> thrift::Result<i32> {
println!("handling calculate: l:{}, w:{:?}", logid, w);
let res = if let Some(ref op) = w.op {
if w.num1.is_none() || w.num2.is_none() {
Err(InvalidOperation {
what_op: Some(*op as i32),
why: Some("no operands specified".to_owned()),
})
} else {
// so that I don't have to call unwrap() multiple times below
let num1 = w.num1.as_ref().expect("operands checked");
let num2 = w.num2.as_ref().expect("operands checked");
match *op {
Operation::Add => Ok(num1 + num2),
Operation::Subtract => Ok(num1 - num2),
Operation::Multiply => Ok(num1 * num2),
Operation::Divide => {
if *num2 == 0 {
Err(InvalidOperation {
what_op: Some(*op as i32),
why: Some("divide by 0".to_owned()),
})
} else {
Ok(num1 / num2)
}
}
}
}
} else {
Err(InvalidOperation::new(
None,
"no operation specified".to_owned(),
))
};
// if the operation was successful log it
if let Ok(ref v) = res {
let mut log = self.log.lock().unwrap();
log.insert(logid, SharedStruct::new(logid, format!("{}", v)));
}
// the try! macro automatically maps errors
// but, since we aren't using that here we have to map errors manually
//
// exception structs defined in the IDL have an auto-generated
// impl of From::from
res.map_err(From::from)
}
fn handle_zip(&self) -> thrift::Result<()> {
println!("handling zip");
Ok(())
}
}

View file

@ -0,0 +1,23 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.
extern crate ordered_float;
extern crate thrift;
extern crate try_from;
pub mod shared;
pub mod tutorial;