@@ -24,7 +24,7 @@ const OBSERVER_RETRY_EXCEPTION: &str = "org.apache.hadoop.ipc.ObserverRetryOnAct
24
24
#[ derive( Debug ) ]
25
25
struct ProxyConnection {
26
26
url : String ,
27
- inner : Option < RpcConnection > ,
27
+ inner : Arc < tokio :: sync :: Mutex < Option < RpcConnection > > > ,
28
28
alignment_context : Arc < Mutex < AlignmentContext > > ,
29
29
nameservice : Option < String > ,
30
30
}
@@ -37,37 +37,42 @@ impl ProxyConnection {
37
37
) -> Self {
38
38
ProxyConnection {
39
39
url,
40
- inner : None ,
40
+ inner : Arc :: new ( tokio :: sync :: Mutex :: new ( None ) ) ,
41
41
alignment_context,
42
42
nameservice,
43
43
}
44
44
}
45
45
46
- async fn get_connection ( & mut self ) -> Result < & RpcConnection > {
47
- if self . inner . is_none ( ) || !self . inner . as_ref ( ) . unwrap ( ) . is_alive ( ) {
48
- self . inner = Some (
49
- RpcConnection :: connect (
50
- & self . url ,
51
- self . alignment_context . clone ( ) ,
52
- self . nameservice . as_deref ( ) ,
53
- )
54
- . await ?,
55
- ) ;
56
- }
57
- Ok ( self . inner . as_ref ( ) . unwrap ( ) )
58
- }
46
+ async fn call ( & self , method_name : & str , message : & [ u8 ] ) -> Result < Bytes > {
47
+ let receiver = {
48
+ let mut connection = self . inner . lock ( ) . await ;
49
+ match & mut * connection {
50
+ Some ( c) if c. is_alive ( ) => ( ) ,
51
+ c => {
52
+ * c = Some (
53
+ RpcConnection :: connect (
54
+ & self . url ,
55
+ self . alignment_context . clone ( ) ,
56
+ self . nameservice . as_deref ( ) ,
57
+ )
58
+ . await ?,
59
+ ) ;
60
+ }
61
+ }
59
62
60
- async fn call ( & mut self , method_name : & str , message : & [ u8 ] ) -> Result < Bytes > {
61
- self . get_connection ( )
62
- . await ?
63
- . call ( method_name, message)
64
- . await
63
+ connection
64
+ . as_ref ( )
65
+ . unwrap ( )
66
+ . call ( method_name, message)
67
+ . await ?
68
+ } ;
69
+ receiver. await . unwrap ( )
65
70
}
66
71
}
67
72
68
73
#[ derive( Debug ) ]
69
74
pub ( crate ) struct NameServiceProxy {
70
- proxy_connections : Vec < Arc < tokio :: sync :: Mutex < ProxyConnection > > > ,
75
+ proxy_connections : Vec < ProxyConnection > ,
71
76
current_index : AtomicUsize ,
72
77
msycned : AtomicBool ,
73
78
}
@@ -80,22 +85,14 @@ impl NameServiceProxy {
80
85
81
86
let proxy_connections = if let Some ( port) = nameservice. port ( ) {
82
87
let url = format ! ( "{}:{}" , nameservice. host_str( ) . unwrap( ) , port) ;
83
- vec ! [ Arc :: new( tokio:: sync:: Mutex :: new( ProxyConnection :: new(
84
- url,
85
- alignment_context. clone( ) ,
86
- None ,
87
- ) ) ) ]
88
+ vec ! [ ProxyConnection :: new( url, alignment_context. clone( ) , None ) ]
88
89
} else if let Some ( host) = nameservice. host_str ( ) {
89
90
// TODO: Add check for no configured namenodes
90
91
config
91
92
. get_urls_for_nameservice ( host) ?
92
93
. into_iter ( )
93
94
. map ( |url| {
94
- Arc :: new ( tokio:: sync:: Mutex :: new ( ProxyConnection :: new (
95
- url,
96
- alignment_context. clone ( ) ,
97
- Some ( host. to_string ( ) ) ,
98
- ) ) )
95
+ ProxyConnection :: new ( url, alignment_context. clone ( ) , Some ( host. to_string ( ) ) )
99
96
} )
100
97
. collect ( )
101
98
} else {
@@ -142,8 +139,6 @@ impl NameServiceProxy {
142
139
let mut attempts = 0 ;
143
140
loop {
144
141
let result = self . proxy_connections [ proxy_index]
145
- . lock ( )
146
- . await
147
142
. call ( method_name, & message)
148
143
. await ;
149
144
0 commit comments