[Performance improvement] Multi-tunnel implementation for native client proxy feature
Bug reports
Steps to reproduce:
- Example code that produces error.
from pssh.clients import ParallelSSHClient
import os
from datetime import datetime
PKEY = os.path.expanduser('~/.ssh/key1')
PROXY_PKEY = os.path.expanduser('~/.ssh/key2')
hosts = ['a.example.com', 'b.example.com', ...]
host_config = {'a.example.com' : { 'user': 'user1','password': 'pass', 'port': 2222, 'private_key': PKEY},
'b.example.com' : {'user': 'user2', 'password': 'pass', 'port': 2223, 'private_key': PKEY},
...
}
print(datetime.now())
print ('Processing %i instances' % (len(hosts)))
client = ParallelSSHClient(hosts,
pool_size= 20,
host_config=host_config,
pkey=PKEY,
proxy_host='proxy.example.com',
proxy_user='ubuntu',
proxy_pkey=PROXY_PKEY)
cmd_response = client.run_command('lsb_release -r', stop_on_errors=False, shell='bash -c')
for node in cmd_response:
print(cmd_response[node]['host'])
print(datetime.now())
- Stack trace or error messages.
Expected behavior: [What was expected to happen.] Similar, if not better times with the native client.
Actual behavior: [What actually happened.] I'm seeing significant slowdown when switching to using the native client with a larger set of hosts. If i run against 3 hosts its lightning fast :). I am using a proxy host and I am unable to test without one.
With the native client, its taking several minutes. 2018-09-17 11:39:21.741385 Processing 154 instances 2018-09-17 11:43:04.167175
Paramiko client takes ~15 seconds 2018-09-17 11:46:22.362456 Processing 154 instances 2018-09-17 11:46:36.419247
Additional info: [Include version of ssh2-python and/or paramiko and any other relevant information.]
parallel-ssh 1.8.1 ssh2-python 0.15.0.post9 paramiko 2.4.1
Hi there,
Thanks for the interest and report.
Yes, this is a known limitation with the current implementation of tunnelling in the native client. It uses a single proxy connection and therefore all clients connections go through the same tunnel, which will slow them down.
I would expect performance to be better with multiple subsequent commands, however. Ie authentication is the most expensive step and subsequent commands on already authentication proxy connections will not be as slow.
The paramiko client uses threads for its proxy connections, which get translated to greenlets by parallel-ssh, meaning authentication can happen more or less in parallel whereas it is serial with the current tunnelling implementation in the native client.
There is a note of this limitation in the issue that implemented this - should also update docs with it though.
Will leave issue open to track implementing multiple proxy connections in the native client to increase performance. This is quite a bit more involved in the native client than it is in paramiko.
The native client has to use threads to do it (real ones) and because sessions cannot be shared across threads the clients using them also have to be created in the same thread.
The plus side is this will increase performance of the native client across the board, whether proxy is used or not, and allow the library to scale even better across multiple cores than it does currently (only authentication is currently offloaded to threads). Do not expect this to be ready before 2.0.0.
Ok cool, good to know. I do think this needs to be stated clearly in the docs. This limitation makes the native client unusable for me. It turns a ~15 minutes process into 1hr+.
@pkittenis I've also found the current implementation to chew CPU once a connection is complete. I was implementing forward tunneling with the intention of reverse tunneling and also dynamic tunnels in my own library and found that the base implementation from parallel-ssh has a this bug.
I opened a connection to ('google.com',80) as a test using curl http://localhost:<port>/ for the testing client. The connection worked but it seems that it proceeded to chew CPU cycles almost the same way a while True: with no limiter would chew a CPU core.
Have not seen that behaviour and have used the current implementation with multiple targets. For reporting issues, code to reproduce needs to be shown.
New implementation in 2.2.0.