-
-
Notifications
You must be signed in to change notification settings - Fork 24
Http client for downloading files, pygrid requests #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
53f9f5d
scaffolding basic job scheduling
vkkhare aae037d
job worker API implemented
vkkhare 72fce30
Added http client. Refactored common api usage
vkkhare 61a91e2
Made socket client and http client uniform in usage
vkkhare 472f7b3
WIP downloader
vkkhare 0f23bb1
added file saver
vkkhare f73b6d9
rebase with dev
vkkhare ec37900
updated standalone-demo
vkkhare 86bff4b
added throwable for jobs and other minor changes
vkkhare a1a4255
added subscriber class for jobs,addressed comments
vkkhare 925bdf3
code clean up
vkkhare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
23 changes: 16 additions & 7 deletions
23
demo-app/src/main/java/org/openmined/syft/demo/StandaloneDemo.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| package org.openmined.syft.demo | ||
|
|
||
| import io.reactivex.Scheduler | ||
| import io.reactivex.android.schedulers.AndroidSchedulers | ||
| import io.reactivex.schedulers.Schedulers | ||
| import org.openmined.syft.Syft | ||
| import org.openmined.syft.networking.clients.SignallingClient | ||
| import org.openmined.syft.networking.requests.Protocol | ||
| import org.openmined.syft.networking.clients.HttpClient | ||
| import org.openmined.syft.networking.clients.SocketClient | ||
| import org.openmined.syft.threading.ProcessSchedulers | ||
|
|
||
| @ExperimentalUnsignedTypes | ||
| fun main() { | ||
| val syft = Syft.getInstance(SignallingClient( | ||
| Protocol.WSS, | ||
| "echo.websocket.org", | ||
| 2000u | ||
| ), object : ProcessSchedulers { | ||
| val networkingSchedulers = object : ProcessSchedulers { | ||
| override val computeThreadScheduler: Scheduler | ||
| get() = Schedulers.io() | ||
| override val calleeThreadScheduler: Scheduler | ||
| get() = AndroidSchedulers.mainThread() | ||
| } | ||
| val computeSchedulers = object : ProcessSchedulers { | ||
| override val computeThreadScheduler: Scheduler | ||
| get() = Schedulers.computation() | ||
| override val calleeThreadScheduler: Scheduler | ||
| get() = Schedulers.single() | ||
| } | ||
| val syft = Syft.getInstance( | ||
| SocketClient( | ||
| "echo.websocket.org", | ||
| 2000u | ||
| , computeSchedulers | ||
| ), HttpClient("echo.websocket.org"), computeSchedulers, networkingSchedulers | ||
| ) | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #Sat Jan 11 12:13:19 GMT 2020 | ||
| #Fri Feb 28 13:29:55 CET 2020 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,85 +1,155 @@ | ||
| package org.openmined.syft | ||
|
|
||
| import android.util.Log | ||
| import io.reactivex.Completable | ||
| import io.reactivex.disposables.CompositeDisposable | ||
| import org.openmined.syft.networking.clients.NetworkMessage | ||
| import org.openmined.syft.networking.clients.SignallingClient | ||
| import org.openmined.syft.networking.datamodels.AuthenticationSuccess | ||
| import org.openmined.syft.networking.datamodels.CycleResponseData | ||
| import org.openmined.syft.networking.datamodels.SocketResponse | ||
| import org.openmined.syft.networking.requests.CommunicationDataFactory | ||
| import org.openmined.syft.networking.requests.REQUESTS | ||
| import org.openmined.syft.networking.clients.HttpClient | ||
| import org.openmined.syft.networking.clients.SocketClient | ||
| import org.openmined.syft.networking.datamodels.syft.AuthenticationSuccess | ||
| import org.openmined.syft.networking.datamodels.syft.CycleRequest | ||
| import org.openmined.syft.networking.datamodels.syft.CycleResponseData | ||
| import org.openmined.syft.networking.requests.CommunicationAPI | ||
| import org.openmined.syft.networking.requests.HttpAPI | ||
| import org.openmined.syft.processes.JobStatusSubscriber | ||
| import org.openmined.syft.processes.SyftJob | ||
| import org.openmined.syft.threading.ProcessSchedulers | ||
| import java.util.concurrent.ConcurrentHashMap | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| private const val TAG = "Syft" | ||
|
|
||
| @ExperimentalUnsignedTypes | ||
| class Syft private constructor( | ||
| private val signallingClient: SignallingClient, | ||
| private val schedulers: ProcessSchedulers | ||
| private val socketClient: SocketClient, | ||
| private val httpClient: HttpClient, | ||
| //todo this will be removed by syft configuration class | ||
| private val computeSchedulers: ProcessSchedulers, | ||
| //todo change this to read from syft configuration | ||
| private val networkingSchedulers: ProcessSchedulers | ||
|
|
||
| ) { | ||
| companion object { | ||
| @Volatile | ||
| private var INSTANCE: Syft? = null | ||
|
|
||
| fun getInstance(signallingClient: SignallingClient, schedulers: ProcessSchedulers): Syft = | ||
| fun getInstance( | ||
| socketClient: SocketClient, | ||
| httpClient: HttpClient, | ||
| networkingSchedulers: ProcessSchedulers, | ||
| //todo this will be removed by syft configuration class | ||
| computeSchedulers: ProcessSchedulers | ||
| ): Syft = | ||
| INSTANCE ?: synchronized(this) { | ||
| INSTANCE ?: Syft( | ||
| signallingClient, | ||
| schedulers | ||
| socketClient, | ||
| httpClient, | ||
| networkingSchedulers, | ||
| computeSchedulers | ||
| ).also { INSTANCE = it } | ||
| } | ||
| } | ||
|
|
||
| private lateinit var workerId: String | ||
| private val workerJobs = ConcurrentHashMap<SyftJob.JobID, SyftJob>() | ||
| private val compositeDisposable = CompositeDisposable() | ||
|
|
||
| private val workerJobs = mutableListOf<SyftJob>() | ||
| private val compositeDisposable = CompositeDisposable().add( | ||
| signallingClient.start() | ||
| .map { | ||
| when (it) { | ||
| is NetworkMessage.SocketOpen -> | ||
| signallingClient.send(REQUESTS.AUTHENTICATION) | ||
| //todo decide if this can be changed by pygrid or will remain same irrespective of the requests we make | ||
|
mccorby marked this conversation as resolved.
|
||
| @Volatile | ||
| lateinit var workerId: String | ||
|
|
||
| is NetworkMessage.SocketClosed -> Log.d( | ||
| TAG, | ||
| "Socket was closed successfully" | ||
| ) | ||
| is NetworkMessage.SocketError -> Log.e(TAG, "socket error", it.throwable) | ||
| is NetworkMessage.MessageReceived -> handleResponse( | ||
| CommunicationDataFactory.deserializeSocket( | ||
| it.message | ||
| ) | ||
| fun newJob( | ||
| model: String, | ||
| version: String? = null | ||
| ): SyftJob { | ||
| val job = SyftJob(this, computeSchedulers, networkingSchedulers, model, version) | ||
| val jobId = SyftJob.JobID(model, version) | ||
| workerJobs[jobId] = job | ||
| job.subscribe(object : JobStatusSubscriber() { | ||
| override fun onComplete() { | ||
| workerJobs.remove(jobId) | ||
| } | ||
|
|
||
| override fun onError(throwable: Throwable) { | ||
| workerJobs.remove(jobId) | ||
| } | ||
| }, networkingSchedulers) | ||
|
|
||
| return job | ||
| } | ||
|
|
||
| fun requestCycle(job: SyftJob) { | ||
| if (this::workerId.isInitialized) | ||
|
mccorby marked this conversation as resolved.
|
||
| socketClient.getCycle( | ||
| CycleRequest( | ||
| workerId, | ||
| job.modelName, | ||
| job.version, | ||
| getPing(), | ||
| getDownloadSpeed(), | ||
| getUploadSpeed() | ||
| ) | ||
| is NetworkMessage.MessageSent -> println("Message sent successfully") | ||
| ).compose(networkingSchedulers.applySingleSchedulers()) | ||
| .subscribe { response: CycleResponseData -> | ||
| when (response) { | ||
| is CycleResponseData.CycleAccept -> handleCycleAccept(response) | ||
| is CycleResponseData.CycleReject -> handleCycleReject(response) | ||
| } | ||
| } | ||
| } | ||
| .subscribeOn(schedulers.computeThreadScheduler) | ||
| .observeOn(schedulers.calleeThreadScheduler) | ||
| .subscribe() | ||
| ) | ||
|
|
||
| fun newJob(modelName: String, version: String): SyftJob { | ||
| val job = SyftJob(modelName, version) | ||
| signallingClient.send( | ||
| REQUESTS.CYCLE_REQUEST, | ||
| CommunicationDataFactory.requestCycle(workerId, job, "", "", "") | ||
| ) | ||
| workerJobs.add(job) | ||
| return job | ||
| else { | ||
| compositeDisposable.add(socketClient.authenticate() | ||
| .compose(networkingSchedulers.applySingleSchedulers()) | ||
| .subscribe { t: AuthenticationSuccess -> | ||
| if (!this::workerId.isInitialized) | ||
| setSyftWorkerId(t.workerId) | ||
| requestCycle(job) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun handleResponse(response: SocketResponse) { | ||
| when (response.data) { | ||
| is AuthenticationSuccess -> | ||
| this.workerId = response.data.workerId | ||
| is CycleResponseData -> { | ||
| when (response.data) { | ||
| is CycleResponseData.CycleAccept -> "accept here" | ||
| is CycleResponseData.CycleReject -> "set timeout for job" | ||
| } | ||
| } | ||
| fun getDownloader(): HttpAPI = httpClient.apiClient | ||
|
|
||
| } | ||
| //todo decide this based on configuration | ||
| fun getSignallingClient(): CommunicationAPI = socketClient | ||
|
|
||
| fun getWebRTCSignallingClient(): SocketClient = socketClient | ||
|
|
||
| @Synchronized | ||
| private fun setSyftWorkerId(workerId: String) { | ||
| if (!this::workerId.isInitialized) | ||
| this.workerId = workerId | ||
| else if (workerJobs.isEmpty()) | ||
| this.workerId = workerId | ||
| } | ||
|
|
||
| private fun getPing() = "" | ||
| private fun getDownloadSpeed() = "" | ||
| private fun getUploadSpeed() = "" | ||
|
|
||
| private fun handleCycleReject(responseData: CycleResponseData.CycleReject) { | ||
| var jobId = SyftJob.JobID(responseData.modelName, responseData.version) | ||
| val job = workerJobs.getOrElse(jobId, { | ||
| jobId = SyftJob.JobID(responseData.modelName) | ||
| workerJobs.getValue(jobId) | ||
| }) | ||
| job.cycleStatus.set(SyftJob.CycleStatus.REJECT) | ||
| compositeDisposable.add( | ||
| Completable | ||
| .timer(responseData.timeout.toLong(), TimeUnit.MILLISECONDS) | ||
| .compose(networkingSchedulers.applyCompletableSchedulers()) | ||
| .subscribe { | ||
| job.cycleStatus.set(SyftJob.CycleStatus.APPLY) | ||
| job.start() | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| private fun handleCycleAccept(responseData: CycleResponseData.CycleAccept) { | ||
| val jobId = SyftJob.JobID(responseData.modelName, responseData.version) | ||
| val job = workerJobs.getOrElse(jobId, { | ||
|
vkkhare marked this conversation as resolved.
|
||
| workerJobs.getValue(SyftJob.JobID(responseData.modelName)) | ||
| }) | ||
| job.setRequestKey(responseData) | ||
| job.downloadData() | ||
| } | ||
|
|
||
|
|
||
| } | ||
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
syftlib/src/main/java/org/openmined/syft/networking/clients/HttpClient.kt
This file contains hidden or 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,17 @@ | ||
| package org.openmined.syft.networking.clients | ||
|
|
||
| import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
| import kotlinx.serialization.json.Json | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import org.openmined.syft.networking.requests.HttpAPI | ||
| import retrofit2.Retrofit | ||
| import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | ||
|
|
||
| class HttpClient(baseUrl: String) { | ||
| val apiClient: HttpAPI = Retrofit.Builder() | ||
| .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||
| .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
| .baseUrl(baseUrl) | ||
| .build().create(HttpAPI::class.java) | ||
|
|
||
| } |
4 changes: 1 addition & 3 deletions
4
syftlib/src/main/java/org/openmined/syft/networking/clients/NetworkMessage.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,7 @@ | ||
| package org.openmined.syft.networking.clients | ||
|
|
||
| sealed class NetworkMessage() { | ||
| object SocketClosed : NetworkMessage() | ||
| sealed class NetworkMessage { | ||
| object SocketOpen : NetworkMessage() | ||
| data class SocketError(val throwable: Throwable) : NetworkMessage() | ||
| object MessageSent : NetworkMessage() | ||
| data class MessageReceived(val message: String) : NetworkMessage() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need two different sets of schedulers? If using just one, wouldn't it work as expected?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is we need different type of thread scheduling for computation and networking. We can spawn multiple threads for networking however compute usually would be restricted to single thread. Then many would require the subscriber to run on mainThread example for network response. While a single compute thread might be busy. This makes the distinction clear for the user and provides larger flexibility