Distributed actors transport example, for feature review

Overview

swift-sample-distributed-actors-transport

Sample application and ActorTransport, associated with distributed actor language evolution proposal.

Running the sample app

  1. Download the latest toolchain from main branch, or a built one from a specific PR. (Alternatively swift-PR-39087-1111-osx.tar.gz, if still available, is a fine toolchain to use for this sample app.)

  2. Move it to Library/Developer/Toolchains/ and point the TOOLCHAIN env variable at it:

export TOOLCHAIN=/Library/Developer/Toolchains/swift-PR-39061-1098.xctoolchain

To run the sample app, use the following command:

cd SampleApp
DYLD_LIBRARY_PATH=$TOOLCHAIN/usr/lib/swift/macosx $TOOLCHAIN/usr/bin/swift run FishyActorsDemo

all necessary flags to build this pre-release feature are already enabled as unsafe flags in Package.swift.

If you wanted to perform the invocation manually, it would look something like this:

export TOOLCHAIN=/Library/Developer/Toolchains/swift-PR-39087-1109.xctoolchain
DYLD_LIBRARY_PATH=$TOOLCHAIN/usr/lib/swift/macosx $TOOLCHAIN/usr/bin/swift run FishyActorsDemo

setting the DYLD_LIBRARY_PATH is important, so don't forget it.

Sample output

The sample is a chat room application. It creates a few "nodes" and starts distributed actors on them.

There are two kinds of actors, a ChatRoom and Chatters. A single node, representing a cloud component, hosts the chat room. And a few other nodes host chatters. Note that chatters can be on the same or on different nodes.

As the application runs, chatters join the remote ChatRoom and say hello there.

The chat room logs whenever a chatter joins the room, or sends a message:

// chat room logs
[:8001/ChatRoom@130A3D1B-...] Chatter [:9003/Chatter@0A2F138C-...] joined this chat room about: 'Cute Capybaras'
[:8001/ChatRoom@130A3D1B-...] Chatter [:9002/Chatter@8152F16D-...] joined this chat room about: 'Cute Capybaras'
[:8001/ChatRoom@130A3D1B-...] Chatter [:9003/Chatter@4F3970DE-...] joined this chat room about: 'Cute Capybaras'
[:8001/ChatRoom@130A3D1B-...] Forwarding message from [:9002/Chatter@8152F16D-...] to 2 other chatters...
[:8001/ChatRoom@130A3D1B-...] Forwarding message from [:9003/Chatter@4F3970DE-...] to 2 other chatters...
[:8001/ChatRoom@130A3D1B-...] Forwarding message from [:9003/Chatter@0A2F138C-...] to 2 other chatters...
[:8001/ChatRoom@130A3D1B-...] Forwarding message from [:9002/Chatter@8152F16D-...] to 2 other chatters...
[:8001/ChatRoom@130A3D1B-...] Forwarding message from [:9003/Chatter@4F3970DE-...] to 2 other chatters...

The chat room sends a "Welcome ..." message to a joining chatter, and forwards all other chat messages sent to the room to the chatter itself. A chatters logs look like this:

// first chatter
[:9002/Chatter@8152F16D-...] Welcome to the 'Cute Capybaras' chat room! (chatters: 2)
[:9002/Chatter@8152F16D-...] Chatter [:9003/Chatter@0A2F138C-...] joined [:8001/ChatRoom@130A3D1B-...] (total known members in room 2 (including self))
[:9002/Chatter@8152F16D-...]] :9003/Chatter@4F3970DE-... wrote: Welcome [:9003/Chatter@0A2F138C-...]!
[:9002/Chatter@8152F16D-...]] :9003/Chatter@0A2F138C-... wrote: Long time no see [:9002/Chatter@8152F16D-...]!
[:9002/Chatter@8152F16D-...] Chatter [:9003/Chatter@4F3970DE-...] joined [:8001/ChatRoom@130A3D1B-...] (total known members in room 3 (including self))
[:9002/Chatter@8152F16D-...]] :9003/Chatter@4F3970DE-... wrote: Hi there,  [:9002/Chatter@8152F16D-...]!

Notice that the simplified ID printout contains the port number of the node the chatter is running on. In this example, the chatroom is running on port 8001 while the chatter is on 9002. Other chatters may be on the same or on different "nodes" which are represented by actor transport instances.

This sample is a distributed application created from just a single process, but all the "nodes" communicate through networking with eachother. The same application could be launched on different physical hosts (and then would have different IP addresses), this is what location transparency of distributed actors enables us to do.

Experimental flags

This project showcases EXPERIMENTAL language features, and in order to access them the -enable-experimental-distributed flag must be set.

The project is pre-configured with a few experimental flags that are necessary to enable distributed actors, these are configured in each target's swiftSettings:

      .target(
          name: "FishyActorTransport",
          dependencies: [
            ...
          ],
          swiftSettings: [
            .unsafeFlags([
              "-Xfrontend", "-enable-experimental-distributed",
              "-Xfrontend", "-validate-tbd-against-ir=none",
              "-Xfrontend", "-disable-availability-checking", // availability does not matter since _Distributed is not part of the SDK at this point
            ])
          ]),

SwiftPM Plugin

Distributed actor transports are expected to ship with an associated SwiftPM plugin that takes care of source generating the necessary "glue" between distributed functions and the transport runtime.

Plugins are run automatically when the project is build, and therefore add no hassle to working with distributed actors.

Verbose mode

It is possible to force the plugin to run in --verbose mode by setting the VERBOSE environment variable, like this:

VERBOSE=true DYLD_LIBRARY_PATH=$TOOLCHAIN/usr/lib/swift/macosx $TOOLCHAIN/usr/bin/swift run FishyActorsDemo

Analyze: /Users/ktoso/code/fishy-actor-transport/SampleApp/Sources/FishyActorsDemo/_PrettyDemoLogger.swift
Analyze: /Users/ktoso/code/fishy-actor-transport/SampleApp/Sources/FishyActorsDemo/Actors.swift
  Detected distributed actor: ChatRoom
    Detected distributed func: join
    Detected distributed func: message
    Detected distributed func: leave
  Detected distributed actor: Chatter
    Detected distributed func: join
    Detected distributed func: chatterJoined
    Detected distributed func: chatRoomMessage
Analyze: /Users/ktoso/code/fishy-actor-transport/SampleApp/Sources/FishyActorsDemo/main.swift
Generate extensions...
WARNING: This is only a *mock* sample plugin implementation, real functions won't be generated!
  Generate 'FishyActorTransport' extensions for 'distributed actor ChatRoom' -> file:///Users/ktoso/code/fishy-actor-transport/SampleApp/.build/plugins/outputs/sampleapp/FishyActorsDemo/FishyActorTransportPlugin/GeneratedFishyActors_1.swift
  Generate 'FishyActorTransport' extensions for 'distributed actor Chatter' -> file:///Users/ktoso/code/fishy-actor-transport/SampleApp/.build/plugins/outputs/sampleapp/FishyActorsDemo/FishyActorTransportPlugin/GeneratedFishyActors_1.swift
You might also like...
SSL/TLS Add-in for BlueSocket using Secure Transport and OpenSSL

BlueSSLService SSL/TLS Add-in framework for BlueSocket in Swift using the Swift Package Manager. Works on supported Apple platforms (using Secure Tran

A Distributed Value Store in Swift.
A Distributed Value Store in Swift.

Impeller is a Distributed Value Store (DVS) written in Swift. It was inspired by successful Distributed Version Control Systems (DVCSes) like Git and

Hyperledger Sawtooth is an enterprise solution for building, deploying, and running distributed ledgers (also called blockchains).
Hyperledger Sawtooth is an enterprise solution for building, deploying, and running distributed ledgers (also called blockchains).

Hyperledger Sawtooth SDK Hyperledger Sawtooth is an enterprise solution for building, deploying, and running distributed ledgers (also called blockcha

Swift Actors Introduction

Swift-Actors-Introduction Swift 5.5~ 並行処理におけるデータ整合やその他の不具合を防ぐための仕組み。 https://doc

Fast iOS app to browse and search movies, tv, actors, credits
Fast iOS app to browse and search movies, tv, actors, credits

Fast iOS app to browse and search movies, tv, actors, credits

A utility that reminds your iPhone app's users to review the app written in pure Swift.
A utility that reminds your iPhone app's users to review the app written in pure Swift.

SwiftRater SwiftRater is a class that you can drop into any iPhone app that will help remind your users to review your app on the App Store/in your ap

This is a Review posting app that let user find interesting places near them
This is a Review posting app that let user find interesting places near them

ColorMatchTabs Inspired by this project on Dribbble Also, read how it was done in our blog Installation CocoaPods pod 'ColorMatchTabs' Carthage github

In-app design review tool to inspect measurements, attributes, and animations.
In-app design review tool to inspect measurements, attributes, and animations.

Hyperion Hyperion - In App Design Review Tool What is it? Hyperion is a hidden plugin drawer that can easily be integrated into any app. The drawer si

Easy BMI calculator to review optionals, segues and structs
Easy BMI calculator to review optionals, segues and structs

BMI Calculator Our Goal The goal of this tutorial is to learn more about Optionals, solidify your understanding of the MVC design pattern and to intro

A framework to provide logic designed to prompt users at the ideal moment for a review of your app/software

ReviewKit ReviewKit is a Swift package/framework that provides logic designed to prompt users at the ideal moment for a review of your app. At a basic

Appirater - A utility that reminds your iPhone app's users to review the app.

Introduction Appirater is a class that you can drop into any iPhone app (iOS 4.0 or later) that will help remind your users to review your app on the

Appstore-Review-Guidelines - A curated list of guideline which has to be taken care before submitting your application to Appstore.

Appstore Review Guidelines The App Review Guidelines provide rules and examples across a range of topics, including user interface design, functionali

A utility application to capture and review search results from Swift Package Index.

SPISearch An app (macOS & iOS) to explore the search results from Swift Package Index. Testflight Links: SPIIndex (iOS and macOS apps) Search Ranking

A modern utility that reminds your iOS app's users to review the app in a non-invasive way.
A modern utility that reminds your iOS app's users to review the app in a non-invasive way.

SiriusRating A modern utility that reminds your iOS app's users to review the app in a non-invasive way. Features SwiftUI and UIKit support Configurab

NintendoSwitch-BezierPath-Example - Nintendo Switch Bezier Path Example

NintendoSwitch-BezierPath-Example An example app that demonstrates the use of Be

Assignment 2 - A fully functional example in the CardinalKit-Example directory
Assignment 2 - A fully functional example in the CardinalKit-Example directory

Assignment 2 - A fully functional example in the CardinalKit-Example directory

AudioPlayer is syntax and feature sugar over AVPlayer. It plays your audio files (local & remote).

AudioPlayer AudioPlayer is a wrapper around AVPlayer. It also offers cool features such as: Quality control based on number of interruption (buffering

Instagram-like photo browser and a camera feature with a few line of code in Swift.
Instagram-like photo browser and a camera feature with a few line of code in Swift.

Fusuma is a Swift library that provides an Instagram-like photo browser with a camera feature using only a few lines of code.

A TextView that provides easy to use tagging feature for Mention or Hashtag
A TextView that provides easy to use tagging feature for Mention or Hashtag

Tagging A TextView that provides easy to use tagging feature for Mention or Hashtag. Introduction Tagging is a UIView that encloses a TextView that co

Comments
  • Restructure project to test SourceGen code generation

    Restructure project to test SourceGen code generation

    I was working on adding source generation but found out it was already contributed in the previous PR, which is pretty cool because now anyone can more easily experiment with their own distributed actors. Since I was already working on this I thought I would at least contribute:

    • a restructuring of the project to make Analysis and SourceGen testable
    • adjust SourceGen to not depend on a file URL, returning the generated code instead as an array of buckets (as a String), so that tests don't have to generate files and read them back
    • main.swift now does the writing of the buckets generated by SourceGen into files by itself
    • add a test for SourceGen
    • adjust test and SourceGen to generate a more consistent formatting of the generated code

    In order to disturb the project as little as possible I restructured the project this way:

    • create a target and a library product by the name of "FishyActorsCore"
    • move Analysis and SourceGen into the new module
    • make the important methods from Analysis and SourceGen public

    I am fine with adjusting the PR if there is a more preferable restructuring of the project to make it more testable.

    Note: I know this is meant as "just" a small example project, but I think making it more real and functional is a good way to demonstrate the idea and allow people to more easily experiment with their own ideas.

    opened by odmir 5
  • update to shipped language feature (remove codegen, experimental things)

    update to shipped language feature (remove codegen, experimental things)

    Swift 5.7 includes distributed actors now, and we no longer need to do any of the experimental tricks, or source generation in order to showcase the feature.

    This implementation is using official released toolchains and is pretty similar to what was presented in https://developer.apple.com/videos/play/wwdc2022/110356/ and previously in this sample repo. I.e. we still use http client and don't really do anything smart, but the focus is about showing the "shape" of an implementation :-)

    opened by ktoso 1
Owner
Apple
Apple
Modular iOS with Uber needle & tuist example

Dodi Modular iOS with Uber needle & tuist example Setup brew install needle bash <(curl -Ls https://install.tuist.io) and run make all Point of concer

David Ha 30 Nov 16, 2022
Example project for the DeDuplicatingEntity Protocol

DeDuplicatingEntity-Sample-Project Example project for the DeDuplicatingEntity Swift Package Clone this repo and build the sample project or use it in

Vic Hudson 0 Feb 16, 2022
React Native Todo List example app which uses Redux for managing app state

react-native-redux-todo-list A sample todo list app developed by using React Native and Redux. How to run the app Install react-native If you don't ha

null 43 Oct 11, 2022
tvOS example app, shows upcoming movies

UpcomingMovies tvOS example app. Shows upcoming movies as a list and in a detail movie screen, using TMDb API. Installation Run pod install on project

Ivan Magda 32 Nov 10, 2022
Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen.

Live Activity Example Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen. Full tutorial available at sparrowcode.i

Sparrow Code 9 Dec 8, 2022
An example implementation of using a native iOS Notification Service Extension (to display images in remote push notification) in Titanium.

Titanium iOS Notification Service Extension An example implementation of using a native iOS Notification Service Extension (to display images in remot

Hans Knöchel 8 Nov 21, 2022
Example of a Flutter app in the status bar.

flutter_in_status_bar Example of a Flutter app in the status bar. This is the default counter app from Flutter but instead of running in a NSWindow it

Jochum van der Ploeg 40 Nov 29, 2022
Review page interaction - handy and pretty way to ask for review.

RPInteraction Overview Review page interaction - handy and pretty way to ask for review. Inspired by dribbble shot. Requirements iOS8 Installation RPI

Nurdaulet Bolatov 27 Jul 16, 2021
Review page interaction - handy and pretty way to ask for review

RPInteraction Overview Review page interaction - handy and pretty way to ask for review. Inspired by dribbble shot. Requirements iOS8 Installation RPI

Nurdaulet Bolatov 27 Jul 16, 2021
SSL/TLS Add-in for BlueSocket using Secure Transport and OpenSSL

BlueSSLService SSL/TLS Add-in framework for BlueSocket in Swift using the Swift Package Manager. Works on supported Apple platforms (using Secure Tran

Kitura 87 Nov 15, 2022